Retrieve ancestor element 2

Source: Internet
Author: User

I have studied this before, but I think the speed is not fast enough. I have done it again. I would like to reiterate the problem. For example, there is a node set [a, B, c, d, e], where A is the parent node of B and D is the parent node of B, the result I want is to remove the included persons in the nodes with the contained relationship. In other words, the final result is [c, d, e].

The contains function may be used here: list them in advance.

 
// Http://www.cnblogs.com/rubylouvre/archive/2009/10/14/1583523.html var contains = function (ancestor, node) {If (node. comparedocumentposition) Return (node. comparedocumentposition (ancestor) & 8) ===8; If (ancestor. contains) return ancestor. contains (node) & ancestor! = Node; while (node = node. parentnode) if (node = ancestor) return true; return false ;}

If a set of ancestor nodes that do not contain a link is obtained, the implementation of the descendant selector and the wildcard selector is crucial because it involves de-duplication and sorting. I didn't understand this in the past. I rushed to get all future generations and sort them again. My new generation of selector cannot go this long way.

Of course, I think like this, I am definitely not the first. For example, Baidu's Fox selector has such a function:

/*** Get the first-level node in the tree set * @ method getfirstlevelnodes * @ static * @ Param {htmlelements | array} tree collection node * @ return {array} returns the first layer array of nodes */Fox. getfirstlevelnodes = function (nodes) {for (VAR I = 1; I

However, this function has a fatal defect, that is, it requires nodes as a node array rather than a node set. You need to know that converting a collection like a node to a pure array is not a problem. The node set like ie cannot easily use array. prototype. slice. call is converted, and empty arrays are put one by one. Pay attention to whether the annotation nodes are mixed in. In addition, it is unrealistic to convert each element node. Therefore, the function obtained by my ancestors must support node sets. Here is my new function:

 // by situ zhengmei gets a collection of ancestor nodes that do not have an inclusive relationship // http://www.cnblogs.com/rubylouvre/archive/2010/12/29/1920358.html#var dom = {uid: 1, getancestor: function (ELS) {var El, node, nodes = [], I = 0, rI = 0, uniqresult = {}; while (El = els [I ++]) {node = El; do {pid = node. uniqueid | (node. uniqueid = Dom. UID ++); If (uniqresult [pid]) {break;} else if (node. nodetype = 9) {// if the file object uniqresult [El. uniqueid] = nodes [ri ++] = El ;}} while (node = node. parentnode) ;}return nodes ;}

<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> get the collection of ancestor nodes that do not contain the relation by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript" charset =" UTF-8> <br/> window. onload = function () {<br/> var arr = document. getelementsbytagname ("Div"); <br/> var Dom ={< br/> uid: 1, <br/> getancestor: function (ELS) {<br/> var El, node, nodes = [], <br/> I = 0, <br/> rI = 0, <br/> uniqresult = {}; <br/> while (El = els [I ++]) {<br/> node = El; <br/> do {<br/> pid = node. uniqueid | (node. uniqueid = Dom. UID ++); <br/> If (uniqresult [pid]) {<br/> break; <br/>} else if (node. nodetype = 9) {// if the document object can be reached <br/> uniqresult [El. uniqueid] = nodes [ri ++] = El; <br/>}< br/>}while (node = node. parentnode); <br/>}< br/> return nodes; <br/>}< br/> var ancestor = Dom. getancestor (ARR) <br/> for (VAR I = 0, n = ancestor. length; I <n; I ++) {<br/> alert (ancestor [I]. ID) <br/>}</P> <p> </SCRIPT> <br/> </pead> <br/> <body> <br/> <Div id = "id1"> 1 <Div id = "ID2"> 2 </div> <Div id = "ID3"> 2 </div> <Div class = "BBB"> 2 </div> </P> <p> <Div id = "ID4"> 1 <Div id = "id5"> 2 </div> <br/> <Div id = "id6" Title = "AAA"> 1 <Div Title = "AAA"> 2 </div> </div> <br/> <Div id = "id7"> <br/> <Div id = "id8"> <strong id = "id9"> strong <Div id = "id10"> 2 </div> </strong> </div> <br/> </body> </P> <p> </ptml> <br/>

RunCode

RelatedArticle: Get the ancestor Element

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.