jquery按xpath选择元素 [英] jquery select element by xpath

查看:236
本文介绍了jquery按xpath选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xpath选择器。如何使用jQuery获取与该选择器匹配的元素?

I have an xpath selector. How can I get the elements matching that selector using jQuery?

我见过 https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript 但它不使用jQuery,它似乎有点过于冗长,我认为它不是跨浏览器。

I've seen https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript but it doesn't use jQuery, and it seems a little too verbose, and I suppose it's not cross-browser.

此外,这个 http://jsfiddle.net/CJRmk/似乎不起作用。

Also, this http://jsfiddle.net/CJRmk/ doesn't seem to work.

alert($("//a").length);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<a href="a1.php"></a>
<a href="a2.php"></a>

推荐答案

document.evaluate()(DOM Level 3 XPath) - 唯一缺少的主要浏览器是MSIE。不过,jQuery支持基本的XPath表达式: http://docs.jquery.com/DOM/Traversing / Selectors#XPath_Selectors (移入当前jQuery版本的插件,请参阅 https:// plugins。 jquery.com/xpath/ )。它只是将XPath表达式转换为等效的CSS选择器。

document.evaluate() (DOM Level 3 XPath) is supported in Firefox, Chrome, Safari and Opera - the only major browser missing is MSIE. Nevertheless, jQuery supports basic XPath expressions: http://docs.jquery.com/DOM/Traversing/Selectors#XPath_Selectors (moved into a plugin in the current jQuery version, see https://plugins.jquery.com/xpath/). It simply converts XPath expressions into equivalent CSS selectors however.

这篇关于jquery按xpath选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆