jQuery 通过 XPath 选择元素 [英] jQuery select element by XPath

查看:29
本文介绍了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) 支持 Firefox、Chrome、Safari 和 Opera - 唯一的主流浏览器缺少的是 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天全站免登陆