Home > Article > Web Front-end > One line of code says goodbye to document.getElementById_javascript trick
Therefore, in most scripts, the DOM is accessed directly through the id of the element.
Later, as various browsers came one after another, they gradually replaced IE. In order to ensure that all sects have unified rules, standardization is increasingly valued. The original method of directly accessing the id was gradually replaced by document.getElementById. If you are still using ID to access elements these days, you will either have to make an embedded web page in the program, or use a super-realm antique website like the Ministry of Railways:) Of course, as long as you make sure that your users use IE to access, such non-standard But it doesn’t hurt to use it.
However, when many third-party browsers first came out, in order to ensure compatibility with a large number of non-standard pages at that time, they retained the non-standard practice of using ID to access the DOM. In fact, among today's mainstream browsers, only FireFox does not support this approach, while Chrome, Opera, Safari, and Mobile Safari all support it.
In this case, we might as well let FireFox also support it, so that all browsers can directly access the DOM using the ID. It is not only fast and convenient, but also reduces cumbersome code and improves operating efficiency.
The implementation is very simple. After the document is loaded, query the element with the id attribute, and then add its reference to the window object: