Introduction to the usage of setAttribute in JavaScript_Basic knowledge
WBOY
Release: 2016-05-16 17:28:22
Original
1428 people have browsed it
setAttribute(string name, string value): Add a new attribute with a specified name and value, or set an existing attribute to a specified value. 1. Style issues The class in setAttribute("class", value) refers to changing the attribute "class", so it needs to be quoted. vName represents assigning a value to the style. For example:
When outputting: , that is, the input control has bordercss style attributes Note: the class attribute is in W3C DOM plays a very important role, but still exists due to browser differences. Using the setAttribute("class", vName) statement to dynamically set the class attribute of Element works in Firefox, but not in IE. Because the browser using the IE core does not recognize "class", it must use "className" instead; Similarly, firefox does not recognize "className" either. So the common method is to use both:
var bar = document.getElementById("testbt"); bar.setAttribute("onclick", "javascript:alert('This is a test!');");
Here we use setAttribute to specify the onclick attribute of e, which is simple and good understand. But IE does not support it. It is not that IE does not support the setAttribute function, but it does not support using setAttribute to set certain attributes, such as object attributes, collection attributes, and event attributes. In other words, using setAttribute to set style and onclick attributes is It doesn't work in IE. In order to achieve compatibility with various browsers, you can use the dot notation method to set the object properties, collection properties and event properties of Element.
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