Adding the name attribute to the element in JavaScript

Source: Internet
Author: User

I encountered a small problem today. During DOM construction, IE cannot pass element. setAttribute ('name', _ variable); and element. name = _ variable; this form adds the name attribute to the element, whether it is IE6 or IE7. (IE8 is acceptable, but IE8rc1 is not acceptable)

Later, I checked MSDN and got the following information:
Copy codeThe Code is as follows:
Internet Explorer 8 and later can set the NAME attribute at run time on elements dynamically created with the createElement method. to create an element with a NAME attribute in earlier versions of Internet Explorer, include the attribute and its value when using the createElement method.

That is to say, we must create a name attribute element by using tags with attributes and values. In order to ensure the compatibility of various browsers, the code can be written as follows:
Copy codeThe Code is as follows:
Var element = null;
Try {
// IE6/IE7 build Mode
Element = document. createElement ('<input name = "radio-button"> ');
} Catch (e ){
// W3C build Method
Element = document. createElement ('input ');
Element. name = 'radio-click ';
}
// Define other attributes
Element. id = 'radio-1'
Element. type = 'Radio ';

I used to be a Java developer. In actual work, the amount of JavaScript code is not much. In some of my own small applications, it is often just a bit of a joke (subconscious) avoid problems, such as cross-origin AJAX and IE Memory leakage. however, after the conversion to UED, JavaScript and interactive applications will certainly become the focus of my future work. There are many opportunities for problems of different sizes (almost every day ), in the process of solving various problems, I felt pain and happy. based on the principle of not doing anything, but not having to worry about it, it is necessary to record it and back it up for yourself to share it with others.

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.