有没有插件可以让我做到这一点?文档说(XPath兼容插件),该功能已经在Jquery版本1.2中被删除,而它链接的插件也已经不存在了!
大多数浏览器支持使用XPath表达式选择元素的document.evaluate()
函数,无需使用jQuery。唯一不支持该功能的主流浏览器是Internet Explorer。然而,Dimitri Glazkov已经创建了一个库来实现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; });