【发布时间】:2014-06-24 07:23:10
【问题描述】:
有插件可以让我这样做吗?它说here (XPath Compatibility Plugin) 在 Jquery 版本 1.2 中删除了该功能,并且它链接的插件消失了!
【问题讨论】:
有插件可以让我这样做吗?它说here (XPath Compatibility Plugin) 在 Jquery 版本 1.2 中删除了该功能,并且它链接的插件消失了!
【问题讨论】:
大多数浏览器都支持document.evaluate() 使用 XPath 表达式选择元素 - 不需要 jQuery。唯一缺乏支持的主要浏览器是 Internet Explorer。不过,Dimitri Glazkov 的 created a library 将实现 IE 缺少的功能。
var result = document.evaluate("//a[@href='#']", document, null, 0, null),
item;
while (item = result.iterateNext()) {
// item will be an <a> element with href="#" here
}
您也可以轻松创建一个插件来包装此功能:
(function($) {
$.xpath = function(exp, ctxt) {
var item, coll = [],
result = document.evaluate(exp, ctxt || document, null, 5, null);
while (item = result.iterateNext())
coll.push(item);
return $(coll);
}
})(jQuery);
// And call it like so:
$.xpath("//a[@href='#']").click(function () { return false; });
【讨论】: