Common cross-browser Dom operations (next, Prev, children)

Source: Internet
Author: User

I have never carefully explored the DOM operations of these browsers before. It is necessary to summarize all these operations.

One is to stop making mistakes in the future;

The second is to avoid looking for questions in the future;

In short, it is necessary to summarize:

Function $ (a) {return document. getelementbyid (a);} // If the node in IE is an inline element (non-style setting: Display: Block), the obtained nextsibling node is a text node (excluding spaces) // eg: // <span id = "TT"> TT </span> <span> AA </span> $ ('TT '). nextsibling. nodetype = 3 // <p id = "TT"> TT </P> <p> AA </P> $ ('TT '). nextsibling. nodetype = 1 // <p id = "TT"> TT </P> mE <p> AA </P> $ ('TT '). nextsibling. nodetype = 3 // Similarly, getprev // gets the next subnode function getnext (EL) {var next = el. nextelements Ibling | El. nextsibling; If (next. nodetype! = 1) return arguments. callee (next); return next;} // obtain the function getprev (EL) {var Prev = El of the previous subnode. previuselementsibling | el. previussibling; If (prev. nodetype! = 1) return arguments. callee (prev); Return Prev;} // obtain the first node function getfirstchild (EL) {var first = el. firstelementchild | el. firstchild; If (first. nodetype! = 1) return arguments. callee (first); return first;} // obtain the last node function getlastchild (EL) {var last = el. lastelementchild | el. lastchild; If (last. nodetype! = 1) return arguments. callee (last); return last;} // obtain all the htmlelement child node functions getchildren (EL) {return el. children;} // obtain all child node functions getchildnodes (EL) {return el. childnodes ;}

That's it.

I have never carefully explored the DOM operations of these browsers before. It is necessary to summarize all these operations.

One is to stop making mistakes in the future;

The second is to avoid looking for questions in the future;

In short, it is necessary to summarize:

Function $ (a) {return document. getelementbyid (a);} // If the node in IE is an inline element (non-style setting: Display: Block), the obtained nextsibling node is a text node (excluding spaces) // eg: // <span id = "TT"> TT </span> <span> AA </span> $ ('TT '). nextsibling. nodetype = 3 // <p id = "TT"> TT </P> <p> AA </P> $ ('TT '). nextsibling. nodetype = 1 // <p id = "TT"> TT </P> mE <p> AA </P> $ ('TT '). nextsibling. nodetype = 3 // Similarly, getprev // gets the next subnode function getnext (EL) {var next = el. nextelements Ibling | El. nextsibling; If (next. nodetype! = 1) return arguments. callee (next); return next;} // obtain the function getprev (EL) {var Prev = El of the previous subnode. previuselementsibling | el. previussibling; If (prev. nodetype! = 1) return arguments. callee (prev); Return Prev;} // obtain the first node function getfirstchild (EL) {var first = el. firstelementchild | el. firstchild; If (first. nodetype! = 1) return arguments. callee (first); return first;} // obtain the last node function getlastchild (EL) {var last = el. lastelementchild | el. lastchild; If (last. nodetype! = 1) return arguments. callee (last); return last;} // obtain all the htmlelement child node functions getchildren (EL) {return el. children;} // obtain all child node functions getchildnodes (EL) {return el. childnodes ;}

That's it.

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.