Differences between Javascript in IE and Firefox

Source: Internet
Author: User

1. Differences in CSS "float" attributes of JavaScript
The basic syntax for getting a specific CSS attribute of a given object is the object. style attribute, and the attributes with hyphens must be replaced by the camel naming method. For example, to obtain the background-color attribute of a div whose ID is "header", we need to use the following syntax:

Document. getElementById ("header"). style. borderBottom = "1px solid # ccc ";

However, because "float" is a reserved word in JavaScript, we cannot use object. style. float to obtain the "float" attribute. Here is how we use these two browsers:
IE Syntax:
Document. getElementById ("header"). style. styleFloat = "left ";

Firefox Syntax:
Document. getElementById ("header" ).style.css Float = "left ";

2. Differences in the computational style of elements in JavaScript
By using the above object. style. property, JavaScript can easily obtain and modify the CSS style of an object. However, the limitation of this syntax is that it can only get the inline style in HTML, or directly use the style set by JavaScript. The style object cannot obtain the styles set using the external style table. To obtain the "calculation style" of an object, use the following code:
IE Syntax:
Var myObject = document. getElementById ("header ");
Var myStyle = myObject. currentStyle. backgroundColor;

Firefox Syntax:
VaR myobject = Document. getelementbyid ("Header ");
VaR mycomputedstyle = Document. defaultview. getcomputedstyle (myobject, null );
VaR mystyle = mycomputedstyle. backgroundcolor;

3. Obtain the "class" attribute of an element.
Similar to the "float" attribute, the two browsers use different Javascript methods to obtain this attribute.
IE Syntax:
VaR myobject = Document. getelementbyid ("Header ");
VaR myattribute = myobject. getattribute ("classname ");

Firefox Syntax:
VaR myobject = Document. getelementbyid ("Header ");
VaR myattribute = myobject. getattribute ("class ");

4. Obtain the "for" attribute of the label.
Like 3, using JavaScript to obtain the "for" attribute of a label also has different syntaxes.
IE Syntax:
VaR myobject = Document. getelementbyid ("mylabel ");
VaR myattribute = myobject. getattribute ("htmlfor ");

Firefox Syntax:
Var myObject = document. getElementById ("myLabel ");
Var myAttribute = myObject. getAttribute ("");

The same syntax is used for the setAtrribute method.
5. Obtain the cursor position
It is rare to obtain the cursor position of an element. If you need to do so, the syntax of IE and Firefox is different. This sample code is quite basic and is generally used as part of processing many complex events. Here it is only used to describe the differences. It should be noted that the results in IE are different from those in Firefox, so this method has some problems. In general, this difference can be compensated by obtaining a "rolling position"-but it is another article.
IE Syntax:
Var myCursorPosition = [0, 0];
MyCursorPosition [0] = event. clientX;
MyCursorPosition [1] = event. clientY;

Firefox Syntax:
Var myCursorPosition = [0, 0];
MyCursorPosition [0] = event. pageX;
MyCursorPosition [1] = event. pageY;

 

 

Http://developer.51cto.com/art/200906/127895.htm

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.