[removed] use xpath in jQuery

前端 未结 8 1358
耶瑟儿~
耶瑟儿~ 2020-12-14 10:16

I have, for example, the next XPath query:

//div[span=\"something\"]/parent::div/child::div[@class=\\\"someClass\\\"]

I want to use this XP

8条回答
  •  清歌不尽
    2020-12-14 10:38

    You could add the results of an existing XPath evaluation to a jQuery selection, I threw together this jquery extension that seems does it all for you.

    Example usage:

    $(document).xpathEvaluate('//body/div').remove()
    

    Here's the add-in.

    $.fn.xpathEvaluate = function (xpathExpression) {
       // NOTE: vars not declared local for debug purposes
       $this = this.first(); // Don't make me deal with multiples before coffee
    
       // Evaluate xpath and retrieve matching nodes
       xpathResult = this[0].evaluate(xpathExpression, this[0], null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    
       result = [];
       while (elem = xpathResult.iterateNext()) {
          result.push(elem);
       }
    
       $result = jQuery([]).pushStack( result );
       return $result;
    }
    

提交回复
热议问题