Compatibility between IE and Firefox and Its Solution (zt)

Source: Internet
Author: User
Compatibility between IE and Firefox and Its Solution (zt)

Original address:Http://hi.baidu.com/thinsoft/blog/item/9d159a1bdb6939f9ae513319.html

Use:
Document. getelementbyid ("apple") is used to access the object, and an ID must be unique on the page.
Document. getelementsbytagname ("Div") [0] access objects by Tag Name

1. setattribute (string name, string value): adds a new attribute with the specified name and value, or sets an existing attribute to the specified value.

To set the attributes of an object, use:
Document. getelementbyid ("apple"). setattribute ("width", "100 ")
Document. getelementsbytagname ("Div") [0]. setattribute ("width", "100 ")
The properties of the access object are as follows:
Document. getelementbyid ("apple"). getattribute ("width ")
Document. getelementsbytagname ("Div") [0]. getattribute ("width ")

We often need to dynamically add various attributes to the element in Javascript, which can be achieved by using setattribute (), which involves the compatibility of the browser.
VaR bar = Document. getelementbyid ("foo ");
Bar. setattribute ("onclick", "javascript: Alert ('this is a test! ');");
Here, setattribute is used to specify the onclick attribute of E, which is simple and easy to understand. However, ie does not support the setattribute function,
You cannot use setattribute to set certain attributes, such as object attributes, set attributes, and event attributes. That is to say, you can use setattribute to set attributes such as style and onclick.
It does not work in IE. To achieve compatibility with various browsers, you can use the dot symbol method to set object attributes, set attributes, and event attributes of the element.
Document. getelementbyid ("foo"). classname = "Fruit ";
Document. getelementbyid ("foo" ).style.css text = "color: # 00f ;";
Document. getelementbyid ("foo"). style. color = "# 00f ";
Document. getelementbyid ("foo"). onclick = function () {alert ("this is a test! ");}

2. About class and classname
Class attributes play an important role in W3C Dom, but they still exist due to browser differences. Use the setattribute ("class", vname) statement to dynamically set
The class attribute of element is okay in Firefox, but not in IE. Because the browser using the IE kernel does not know "class", you should use "classname" instead ";
Similarly, Firefox does not know "classname ". Therefore, the common methods are both:
Element. setattribute ("class", vname );
Element. setattribute ("classname", vname); // for IE

About the problem that the table cannot Insert a new row in IE
In IE, neither innerhtml nor appendchild can be inserted into the table <tr>, but other browsers are normal. The solution is to add <tr> to the <tbody> element of the table, as shown below:

VaR ROW = Document. createelement ("TR ");
VaR cell = Document. createelement ("TD ");
VaR cell_text = Document. createtextnode ("bananas do not eat apples ");
Cell. appendchild (cell_text );
Row. appendchild (cell );
Document. getelementsbytagname ("tbody") [0]. appendchild (ROW );

Window. Event

IE: window. event object
FF: There is no window. event object. You can pass the event object to the function parameters. For example, onmousemove = domousemove (Event)

Current mouse Coordinate
IE: event. X and event. Y.
FF: event. pagex and event. Pagey.
Generic: both have the event. clientx and event. clienty attributes.

The current coordinates of the mouse (plus the scroll bar rolling distance)
IE: event. offsetx and event. offsety.
FF: event. layerx and event. layery.

Coordinates of tag X and Y: style. posleft and style. postop
IE: Yes.
FF: No.
Generic: object. offsetleft and object. offsettop.

Height and width of the form
IE: Document. Body. offsetwidth and document. Body. offsetheight. Note: The page must have a body tag.
FF: always specify innerwidthand always specify innerhegihtto use document.documentelement.clientwidthand document.doc umentelement. clientheight.
Generic: Document. Body. clientwidth and document. Body. clientheight.

Add event
IE: element. attachevent ("onclick", func );.
FF: element. addeventlistener ("click", func, true ).
Generic: element. onclick = func. Although the onclick event can be used, the effect of onclick is different from that of the above two methods. onclick only executes one process, while attachevent and addeventlistener execute one process list, that is, multiple processes. For example, both func1 and func2 are executed in element. attachevent ("onclick", func1); element. attachevent ("onclick", func2.

Custom Attributes of tags
IE: If you define an attribute value for the label div1, you can obtain this value using div1.value and div1 ["value.
FF: div1.value and div1 ["value"] cannot be used.
General purpose: div1.getattribute ("value ").

Parent node, child node, and delete node
IE: parentelement, parement. Children, element. romovenode (true ).
FF: parentnode, parentnode. childnodes, node. parentnode. removechild (node ).

CSS: transparent
IE: filter: progid: DXImageTransform. Microsoft. Alpha (style = 0, opacity = 60 ).
FF: opacity: 0.6.

Set the CSS style
Document.getelementbyid('look').style.css text = "display: none;"; // common
Document. getelementbyid ('look'). setattribute ("style", "display: none;"); // Firefox

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.