2015.7.12JS-11 (Dom Foundation)

Source: Internet
Author: User

1.childNodes, gets the child node, itself is an array, you can use subscript childnodes[i] to get a sub-node.

alert (obj.childNodes.length); The presence of a blank node in an advanced browser

// the loop determines whether the element node is based on the NodeType type of the node
for (var i = 0, len = aElement.childNodes.length; i < Len; i++) { if(aelement.childnodes[i].node  Type = =1 = "Red"; }}

2. Child nodes

Firstchild,firstelementchild;

Lastchild,lastelementchild;

3. Sibling nodes

previoussibling,previouselementsibling;

nextsibling,nextelementsibling;

// use if to Judge FirstChild and Firstelementchild to be compatible if  = "Red";} Else  = "Red";}

Other nodes in the same vein

// Remove white space nodes to be compatible with FirstChild function Cleanwhitespace (Element) {  for (var i = 0; i < element.chiledNodes.length; i++) {var node = element.childnodes[i]; // loops the current child node if    (Node.nodetype = = 3 &&!/\s/. Test (Node.nodevalue)) {node.parentNode.removeChild ();  //\s matches any visible character when the node is a text node with Yes! /\s/, non-visible characters, delete these nodes  }}    

4. Parent element ParentNode

// Click the current a tab to find the parent of the a tag, and then hide  var arrA = document.getElementsByTagName ("a");  for (var i = 0, len = arra.length; i < len; i++function() {  th is. ParentNode.style.display = "None" }}

5. Selecting Elements with classname

// Choose Li with class as box to color it var aLi = document.getElementsByTagName ("li");  for (var i = 0, len = ali.length; i < Len; i++) {  if(ali[i].classname = = "box") { 
   
    this. Style.background = "Red"
    ; }}
   

//PackagefunctionGetbyclass (objparent,targetclass) {varAresult = []; varAelement =objparent.getelementsbytagname ("*");  for(vari = 0, len = aelement.length; i < Len; i++) {if(Aelement[i].classname = =Targetclass)    {Aresult.push (aelement[i]); }}returnAresult; Returns the array with the box as class}//call var Abox = Getbyclass (Anav, "box"); Get an array of class as Box for(vari = 0, len = abox.length; i < Len; i++) {Abox[i].style.background= "Red";}

2015.7.12JS-11 (Dom Foundation)

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.