Home  >  Article  >  Web Front-end  >  Find the script attribute corresponding to the html tag_Experience exchange

Find the script attribute corresponding to the html tag_Experience exchange

WBOY
WBOYOriginal
2016-05-16 12:10:261393browse

In JavaScript, you can create and reference an element, and then get or set attributes through this element. However, the attributes of an element do not correspond to the attributes used in the script. A relatively remote attribute, even Google has a hard time finding its corresponding script attribute. I encountered this problem today: I need to create a label to correspond to a check box, so that clicking the label can also select the check box. This is a very common technique, such as the following code:



can make the check box selected when clicking the word "Top" box.
Under normal circumstances, this code is written directly to the file and is easy to complete. When I need to create it through a script, I encounter a problem. Generally, the script attributes and the attributes written in the tag are the same. For example, the target attribute href of the link tag a is consistent. But because for is a reserved word in JavaScript, it cannot be used as an attribute. I searched online for a long time but couldn't find what the script attribute corresponding to for is, so I came up with the following method to find the script attribute corresponding to a label attribute. Now let's take the script attribute corresponding to the for attribute of label as an example:



The principle is this. I first write an html tag into the document, and then assign a unique value to the attribute I am looking for. In the example, it is for label. Attribute copy "abcd", then reference this element through javascript, and traverse all its attributes. If the value of an attribute is equal to abcd, then the name of this attribute can be considered to be its corresponding script attribute. It was through this method that I discovered that the script attribute corresponding to the for attribute of label should be: htmlFor. Hehe, it’s still quite weird. A similar method can also be used to find css script properties. You may wish to try it.
<script> <BR> var lbl=document.getElementById("lblTest"); <BR> for(var p in lbl){ <BR> var s=eval("lbl."+p); <BR> if(s=="abcd")alert(p); <BR> } <BR></script>

Statement:
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