This article mainly describes the JavaScript through the element ID and name directly to get elements of the method, involving JavaScript to get elements of the relevant skills, very practical value, the need for friends can refer to the
The examples in this article describe the way JavaScript directly obtains elements through element IDs and name. Share to everyone for your reference. The specific analysis is as follows:
We know some of the third party JS Library on how to quickly select the elements in HTML to do some simplification, seemingly very inscrutable, in fact, otherwise. And JS itself with a special element of the simple selection of methods, the following for everyone a brief introduction.
In HTML, the most straightforward way to identify HTML elements is to use the name and ID attributes, which are subtly different: IDs must be unique to the page, but name can have duplicates.
In JS, if the ID name does not have the same name as a built-in property or global variable, it automatically becomes the property of the Window object, and in the top-level environment of an HTML page:
?
So if we write an HTML element code like this, we can refer to it as follows:
?
1 2 3 4 5 |
<input type= "button" id= "Btn_ok" value= "OK" onclick= "..."/>//Can be so referenced btn_ok.onclick = function () {}; Or the following is the same Window.btn_ok.style = ...; |
For the Name property, only certain types of HTML elements have similar methods, such as Form,img,iframe,applet,embed,object. In these elements, the elements of a particular Name property can be accessed through the properties of the global variable or document, and if there are multiple elements of the same name property, a NodeList object resembling a read-only group, such as the following code, is returned:
?
1 2 3 4 5 6 7 8 9 10 11 |
<div> </div>//We can quote the elements named Pic: for (x in pic) console.log (Pic[x].alt); or non-"standard" syntax each statement way for each (img in pic) console.log (Img.alt); |
I hope this article will help you with your JavaScript programming.