Compatibility of SetAttribute

Source: Internet
Author: User

Class and ClassName Compatibility methods:

Object.setattribute ("Class", "content")

Can be set successfully in IE8, Chrome, Firefox, Opera10, but cannot be set under IE7.

Object.setattribute ("ClassName", "content")

Only IE7 can be set successfully, but other browsers cannot.

Compatibility method:

Use object.classname= "Content"

Style and Csstext compatibility methods:

Object.setattribute ("Style", "position:absolute;left:10px;top:10px;")

Can be set successfully in IE8, Chrome, Firefox, Opera10, but cannot be set under IE7.

Object.setattribute ("Csstext", "position:absolute;left:10px;top:10px;")

This setting method is not supported by all browsers.

Compatibility method:

Use object.style.csstext= "position:absolute;left:10px;top:10px;"

or separate object.style. Each property is set individually.

Firefox and IE JS compatibility: Set element style familiar

Under IE, the object, collection and event properties of the SetAttribute set element are only used as normal properties, but can be assigned directly, as follows:

var csstext = "font-weight:bold;color:red;"
Use the following for Firefox type browser
Element.setattribute ("style", csstext);

The following wording is used for IE type browser
Element.style.cssText = Csstext;

Issues such as method properties
For example:
var bar = document.getElementById (TESTBT);
Bar.setattribute (onclick, Javascript:alert (' This is a test! '););
Here, we use setattribute to specify the onclick property of E, which is simple and well understood.
But 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, setting style and onclick with setattribute is not feasible in IE.

To achieve compatibility with various browsers, you can use dot notation to set the element's object properties, collection properties, and event properties.
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!);

SetAttribute compatibility

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.