How to add a name attribute to an element in JavaScript_javascript tips
WBOY
Release: 2016-05-16 18:06:55
Original
1332 people have browsed it
I encountered a small problem today. When building the DOM, IE cannot add the name attribute to the element through element.setAttribute('name', _variable); and element.name = _variable;, whether it is IE6 or IE7. . (IE8 is ok, but IE8rc1 is not)
Later I checked MSDN and got the following information:
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 an element with a name attribute through a tag with an attribute and a value. To ensure compatibility across browsers Good, the code can be written like this:
var element = null; try { // IE6/IE7 construction method element = document.createElement(''); } catch (e) { // W3C construction method element = document.createElement('input'); element.name = 'radio-button'; } // 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 actually not much. In some small applications, they often just make small fuss and (subconsciously) avoid some possible problems. Issues such as cross-domain use of AJAX and IE memory leaks are rarely considered. But after switching to UED, JavaScript And interactive applications will definitely become the focus of my future work. There are quite a lot of opportunities for problems of various sizes to occur (almost every day now). In the process of solving various problems, I feel pain and happiness. In the spirit of not messing around, In line with the principle of less fuss, it is necessary to record these for your own backup and to share them with others.
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