Developing Cross-browser JavaScript Method Descriptions 1th/2 page _javascript tips

Source: Internet
Author: User
Developing cross-browser JavaScript

1. ChildNodes the difference between FF and IE.

Node (NodeType = 1) in FF is separated by Textnode (NodeType = 3), and ie/op is not.

<div id= "Box1" ><span>content</span></div>

Under FF, the childnodes of Box1 is 3, IE is 1.

2. Set the style class name of a node object.

In IE, the class of a node is set with "ClassName" as the attr to set or get.

Other browsers, such as FF, use "class" as attr to set or get.

Code:

if (typeof Node1.getattribute ("className") = = "string") {

.

}

3. Set the style content of a node object.

The direct example

Code:

var ostyle = Onode.getattribute ("style");

Ie

if (Ostyle = = "[Object]") {

Ostyle.setattribute ("Csstext", Strstyle);

Onode.setattribute ("style", ostyle);

} else {

Onode.setattribute ("style", strstyle);

}

4. Event object.

IE with Event

FF with evnt

5. Event Action Object

IE with objevent.srcelement

FF with Objevent.target

This is related to XML file writing, the IE PreserveWhitespace set to True look at the bottom is taken from Microsoft's documentation
Code:

var xmldoc = new ActiveXObject ("msxml2.domdocument.4.0");

Xmldoc.async = false;

Xmldoc.preservewhitespace = true;

Xmldoc.load ("books.xml");

alert (xmldoc.xml);

Xmldoc.async = false;

Xmldoc.preservewhitespace = false;

Xmldoc.load ("books.xml");

alert (xmldoc.xml);

-----------------------

1. Append rows to the table:

The Document.createelement and Document.appendchild methods make it easy to append rows to a table or create a new table containing table rows from scratch: using Document.createelement Create tables, add these table cells to the table rows using the Document.appendchild method, and then add the table rows to the table using Document.appendchild.

IE allows you to add the TR element to the tbody instead of adding it directly to the table.

<table id= "MyTable" >

<tbody id= "Mytablebody" ></tbody>

</table>

The correct way to add rows to this table is to add the rows to the table body, not to the table, as shown:

var cell=document.createelement ("TD"). AppendChild (document.createTextNode ("foo");

var row = document.createelement ("tr"). appendchild (cell);

document.getElementById ("Mysqltablebody"). appendchild (row);

Fortunately, this approach is common in all current browsers, including IE. If you get into the habit of using the table's body, you don't have to worry about it.

2 style of elements by javascrīpt

You can set the style of an element by javascrīpt using the setattribute method of the element. For example, to modify the text in a SPAN element to appear in bold red, you can use the SetAttribute method as follows:

var spanelement = document.getElementById ("MySpan");

Spanelement.setattribute ("Style", "font-weight:bold; color:red;");

In addition to IE, this approach is available in other browsers today. For IE, the workaround is to use the Csstext property of the element style object to set the desired style, although this property is not standard but widely supported, as follows:

var spanelement = document.getElementById ("MySpan");

SpanElement.style.cssText = "Font-weight:blod; color:red; ";

This approach works well on IE and most other browsers, except Opera. To make your code portable in all current browsers, you can use both methods, either by using the SetAttribute method and by using the Csstext property of the style pair, as follows:

var spanelement = document.getElementById ("MySpan");

Spanelement.setattribute ("Style", "font-weight:bold; color:red;");

SpanElement.style.cssText = "Font-weight:blod; color:red; ";
Current 1/2 page 12 Next read the full text
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.