Tips for using JS to operate page tables and elements

Source: Internet
Author: User

(1)
The operations on table labels vary with ie, firefox, and other browsers. in ie, assigning values to innerHTML of table and tr is not allowed. When adding a tr using js, the appendChile method does not work. The following is my test results for the three browsers:

InsertRow

IE6: supported, and the default parameter is-1. It is added to the end by default.

FireFox: supported, but the default parameters are supported.

Opera: supported. default parameters are supported. It is added to the beginning by default.


AppendChild

IE6: not supported

FireFox: Yes, but adding TR does not affect ROWS.

Opera: supported. The effect is the same as that of insertRow (-1), which affects ROWS.

By following the specifications to the maximum extent, you can write safe and applicable code:

// Append an empty row to the table:
Var otr = otable. insertRow (-1 );
Var otd = document. createElement ("td ");
Otd. innerHTML = "";
Otd. className = "XXXX ";
Otr. appendChild (otd );

In this way, you can run on these three browsers.
(3) childNodes operations
(1) attribute nodeName
Utils. getChildrenByTagName = function (node, tagName ){
Var ln = node. childNodes. length;
Var arr = [];
For (var z = 0; z <ln; z ++ ){
If (node. childNodes [z]. nodeName = tagName ){
Arr. push (node. childNodes [z]);
}
}
Return arr;
};
(2) Property id
Function getNodeID (parent, id ){
Var ln = parent. childNodes. length;
For (var z = 0; z <ln; z ++ ){
If (parent. childNodes [z]. id = id ){
Return parent. childNodes [z];
}
}
Return null;
}
(3) attribute className
Corresponding class, such as <tr class = "class1">
Function getElementsByClassName (node, className ){
Var children = node. getElementsByTagName ("*");
Var elements = new Array ();
For (var I = 0; I <children. length; I ++ ){
Var child = children [I];
Var classNames = child. className. split ("");
For (var j = 0; j <classNames. length; j ++ ){
If (classNames [j] = className ){
Elements. push (child );
Break;
}
}
}
Return elements;
}

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.