Introduction to the attribute and setattribute usage of javascript

Source: Internet
Author: User

To start with setattribute, we often need to add various properties to the element dynamically in JavaScript, which can be done by using setattribute (), which involves browser compatibility issues.

The code is as follows Copy Code

1 setattribute (string name, String value):

Adds a new property that specifies the name and value, or sets an existing property to the specified value.

1, Style problems

The code is as follows Copy Code
SetAttribute ("Class", value)

The middle class refers to changing the attribute of "class", so you have to quotation.
VName represents the assignment of a style.
For example:

The code is as follows Copy Code

var input = document.createelement ("input");

Input.setattribute ("type", "text");

Input.setattribute ("name", "Q");

Input.setattribute ("Class", bordercss);


When output:, that is, the input control has BORDERCSS style properties
Note: The class attribute plays an important role in the Web-consortium DOM, but it still exists because of browser variability.
Dynamically setting the class attribute of an element using setattribute ("Class", VName) statement is a good way to do it in Firefox, but not in IE. Because the browser using IE kernel does not know "class", to switch to "className";
Also, Firefox does not know "className". So the common method is both:

The code is as follows Copy Code

Element.setattribute ("Class", value); For Firefox

Element.setattribute ("ClassName", value); For IE

2, method attributes and other issues

For example:

The code is as follows Copy Code

var bar = document.getElementById ("TESTBT");
Bar.setattribute ("onclick", "Javascript:alert (' a test! ');");

Here, using setattribute to specify the onclick properties of E, simple, very good understanding.
However, IE does not support, IE does not support setattribute this function, but does not support the use of setattribute set some properties, such as Object properties, collection properties, event properties, In other words, using setattribute to set style and onclick properties is not feasible in IE.

To achieve compatibility with various browsers, you can set the element's object properties, collection properties, and event properties by using the dot notation method.

The code is as follows Copy Code

document.getElementById ("TESTBT"). ClassName = "Bordercss";

document.getElementById ("TESTBT"). Style.csstext = "color: #00f;";

document.getElementById ("TESTBT"). Style.color = "#00f";

document.getElementById ("TESTBT"). onclick= function () {alert ("This is a test!");}


This extends to the problem:

An input text, when the HTML assigned to a DIV innerHTML, encountered a phenomenon, when under the Firefox (ie does not exist this problem), after the assignment of the innerHTML does not contain value, that is, when you enter the text box after the content,
You want to

The code is as follows Copy Code

1 <input type=text name=input1 value= "AA" >


When assigned to a Div, you will only get

The code is as follows Copy Code

1 <input type=text name=input1>

This always clears the value.

At this time, setattribute function, add in the input: onkeyup= "This.setattribute (' value ', This.value)", that is, dynamic input control plus value, The text box is then assigned to Div,value and will not be emptied.

Attributes are generally available to get the value of a property, such as

The code is as follows Copy Code

document.getElementById ("TESTBT"). attribute[' src '].value;

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.