Despite the lengthy and annoying use of Javascript in historyCodeThe period of a specific browser with a block to mark has ended, but it is still necessary to occasionally use some simple code blocks and object detection to ensure that some code works normally on the user's machine.
This articleArticleInternet Explorer and Firefox have seven different aspects in Javascript syntax.
1. CSS "float" attributes
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. Calculation style of Elements
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;
6. Obtain the size of the window or browser window
Sometimes you need to find out the size of the valid window space of the browser, which is generally "window ".
IE Syntax:
VaR mybrowsersize = [0, 0];
Mybrowsersize [0] = document.doc umentelement. clientwidth;
Mybrowsersize [1] = document.doc umentelement. clientheight;
Firefox Syntax:
VaR mybrowsersize = [0, 0];
Mybrowsersize [0] = Window. innerwidth;
Mybrowsersize [1] = Window. innerheight;
7. Alpha transparency
Well, this is not actually a javascript syntax project-Alpha transparency is set through CSS. However, when the object is set to fade in and out through JavaScript, it needs to be implemented by obtaining the Alpha setting of CSS, which is generally within the loop. Use the following JavaScript to change the CSS code:
IE Syntax:
# Myelement {
Filter: alpha (opacity = 50 );
}
Firefox Syntax:
# Myelement {
Opacity: 0.5;
}
To obtain these values using JavaScript, you need to use the style object:
IE Syntax:
VaR myobject = Document. getelementbyid ("myelement ");
Myobject. style. Filter = "alpha (opacity = 80 )";
Firefox Syntax:
VaR myobject = Document. getelementbyid ("myelement ");
Myobject. style. Opacity = 0.5 ″;
Of course, we have already mentioned that it is generally used to change opcity/Alpha in the middle of a loop to create an animation effect. But this is a simple example, to clearly describe how the method is implemented.