Methods for adding the name attribute to an element in JavaScript _javascript tips

Source: Internet
Author: User
Today, there is a small problem, in the building DOM, IE can not pass Element.setattribute (' name ', _variable); and element.name = _variable; In this form, add the name attribute to the element, whether it is IE6 or IE7. (IE8 is OK, but ie8rc1 not)

I later looked at MSDN and got the following information:
Copy Code code as follows:

Internet Explorer 8 and later can set the ' NAME ' at ' run time ' elements dynamically created with the Createelemen T method. To create an element with a NAME attribute in earlier versions of Internet Explorer, include the attribute and its value W Hen using the CreateElement method.

That is, we have to create an element with the name attribute through a label with attributes and values. To make the browsers compatible, the code can write this:
Copy Code code as follows:

var element = null;
try {
How to build Ie6/ie7
element = Document.createelement (' <input name= ' Radio-button ' > ');
catch (e) {
How the consortium is built
element = document.createelement (' input ');
Element.name = ' Radio-button ';
}
Define additional properties
element.id = ' radio-1 '
Element.type = ' Radio ';

I used to be a Java developer, the actual work of JavaScript code is not much, in some of their own small applications are often just a little bit, will (subconsciously) avoid some possible problems, such as the cross-domain use of AJAX, IE memory leaks such issues rarely go back to consider. But after the UED, JavaScript and interactive apps will definitely become my focus later, there are quite a few opportunities for different sizes (now almost every day), and I feel pain and joy in the process of solving various problems. In the spirit of not fooling, less toss the principle, it is necessary to record these, for their own backup, more able to share with others.
Related Article

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.