Find script attributes for HTML tags _ experience Exchange

Source: Internet
Author: User
Tags html tags
In JavaScript, you can create and reference an element, and then get or set the property by this element. However, the attributes of an element are not corresponding to the attribute one by one used in the script. A more remote attribute, even Google is hard to find its corresponding script properties. Today I have this problem: I need to create a label tag to correspond to a check box so that clicking the label tag also selects the check box. This is a very common technique, such as the following code:

<input type= "CheckBox" id= "Cbtop"/>
<label for= "Cbtop" > Top </label>
You can also select the check box when you click on "Top" two words.
Under normal circumstances, this code is written directly into the file, it is easy to complete. When I needed to create a script, I ran into a problem, and the generic script attributes were the same as the attributes written in the tag, such as the target attribute of link tag A, and href was consistent. However, because for is a reserved word in JavaScript, it cannot be a property. I've been searching the internet for a long time and I didn't find a for the corresponding script attribute, so the following method was developed to find a script attribute corresponding to the tag attribute, for example, the script attribute for the label's for attribute is now used:
<label for= "ABCD" id= "Lbltest" >test</label>
<script type= "Text/javascript" >
var Lbl=document.getelementbyid ("Lbltest");
For (var p in lbl) {
var s=eval ("LbL." +P);
if (s== "ABCD") alert (p);
}
</script>
The idea is that I write an HTML tag into a document and then assign a unique value to the property you're looking for by copying "ABCD" to the label's for property, and then referencing the element through JavaScript, and traversing all of its properties, if the value of one property equals ABCD, Then you can assume that the name of this property is its corresponding script attribute. This is the way to find the label for the property corresponding to the script attribute should be: htmlfor. Hey, still more weird oh. Similar methods can also be applied to find CSS script properties, you may wish to try.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.