Usage instructions for javascript getElementsByName()_Basic knowledge
WBOY
Release: 2016-05-16 18:48:54
Original
1317 people have browsed it
Let's take a look: oEle= document.all.aaa ;//There is an aaa object here, but we don't know what its current length is, so we can't operate on it. Therefore, we need to judge the length first The process is as follows: if(oEle.length){}else{}; In both cases, the content in the curly braces is written differently:
But isn’t it too complicated to write like this? And when there is a lot of code in the curly braces, we have to write the code twice. I’m confused~ Fortunately, there is document.getElementsByName() is the method. It handles one and multiple the same way. We can use: oEle = document.getElementsByName('aaa') to reference When there is only 1 oEle, Then it is oEle[0]. When there are multiple ones, use the subscript method oEle[i] to obtain them in a loop. Isn’t it very simple? It is worth mentioning that it is equally effective for Name and ID. But it can only be applied to document objects. Correspondingly, there is another method that can be applied to a wider range of objects: getElementsByTagName. For example, I know a
< input/>...
I want to get all the inputs in the DIV, just write this: aaa.getElementsByTagName('INPUT'), this way The valid one can be distinguished from other DIVs (for example, there is a DIV called bbb, which has the same input). Corresponding to getElementsByTagName, there is also document.body.all.tags(), which can be used The object of this method is much smaller than getElementsByTagName, but more than getElementsByName. Here we also need to mention getElementById, which can only be used by document objects, and returns the first element of the array, haha, Its method name clearly states that it is getElement instead of getElements, so don’t get confused.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn