How to handle the incompatibility of float class width and height assignment of js CSS (ie7, ie8, firefox)

Source: Internet
Author: User
Tags reserved versions

"Float" attribute of css

[Analysis description] the most basic syntax for accessing a given css value through special webpage effects is: object. style. property, but some css attributes have the same name as the reserved words in the special effect on a webpage, such as "float", "for", and "class". Different browsers use different methods.

Write in ie as follows:

Document. getelementbyid ("header"). style. stylefloat = "left ";
Write in firefox as follows:

Document. getelementbyid ("header" ).style.css float = "left ";
[Compatible processing] add a judgment before writing to determine whether the browser is ie:

If (document. all) {document. getelementbyid ("header "). style. stylefloat = "left";} else {document. getelementbyid ("header" ).style.css float = "left ";}
 

2. Access "for" in the <label> label"

[Analysis description] similar to the "float" attribute, you also need to access "for" in the <label> label using non-existent Syntax differentiation ".

Write in ie as follows:

Var myobject = document. getelementbyid ("mylabel"); var myattribute = myobject. getattribute ("htmlfor ");
Write in firefox as follows:

Var myobject = document. getelementbyid ("mylabel"); var myattribute = myobject. getattribute ("");
[Compatible processing] The solution is to determine the browser type first.

 

3. Access and set class attributes

[Analysis description] because the class is reserved for javascript, the two browsers use different javascript methods to obtain this attribute.

For all ie versions earlier than ie8.0:

Var myobject = document. getelementbyid ("header"); var myattribute = myobject. getattribute ("classname ");
For ie8.0 and firefox:

Var myobject = document. getelementbyid ("header"); var myattribute = myobject. getattribute ("class ");
In addition, when setattribute () is used to set the class attribute, the two browsers also have the same difference.

Setattribute ("classname", value );

This method applies to all ie versions earlier than ie8.0. Note: ie8.0 does not support the "classname" attribute.

Setattribute ("class", value); applicable to ie8.0 and firefox.

[Compatible processing]

Method 1 and both are written as follows:

Var myobject = document. getelementbyid ("header"); myobject. setattribute ("class", "classvalue"); myobject. setattribute ("classname", "classvalue"); // set the class of the header to classvalue
Method 2: both ie and ff support object. classname, so you can write as follows:

Var myobject = document. getelementbyid ("header"); myobject. classname = "classvalue"; // set the class of the header to classvalue
Method 3: first determine the browser type, and then use the corresponding method based on the browser type.

 

4. Assignment of object width and height

[Analysis description] The statement similar to obj. style. height = imgobj. height in firefox is invalid.

[Compatible processing] use obj. style. height = imgobj. height + 'px ';

>

 

Related Article

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.