编辑:使用bind('click')绑定(bind)的事件似乎也会出现同样的问题。
无论我点击链接本身还是它的任何子元素,如何防止 live() 绑定(bind)链接被跟踪?
具有以下标记:
<a class='dynamiclink' href='file.htm'>Some text <span> more text </span></a>
还有一些 javascript(使用 jQuery):
$('a.dynamiclink').live('click', function (e) {
e.preventDefault(); // only valid for when the target was the link
// do stuff
return false; // dont follow the link. Also prevent further bubbling for live()
});
如果我们单击该链接,我们的 e.target 将包含该链接,并且 e.preventDefault() 将阻止该链接被跟踪。 但是,如果我们单击 span 标记,e.target 将包含它,而 e.preventDefault() 不会执行任何有用的操作。
返回 false 似乎会阻止除 ie6 和 ie7(ie8 未测试)之外的所有浏览器跟踪该链接,但我需要 ie6/7 才能工作。
如果有帮助的话,提供给 live() 的事件似乎是 jQuery 为 live() 方法专门制作的。它缺乏常规事件对象的大部分属性,例如originalTarget。 关于 jQuery 和 live() 事件 http://api.jquery.com/live/
最佳答案
尝试:
$('a.dynamiclink').live('click', function() {
return false;
}).children().click(function() { // or .find('span').click(function() {
return false;
});
或者:
$('a.dynamiclink, a.dynamiclink li').live('click', function() {
return false;
}).
关于jquery - 单击链接子元素时如何防止 IE 中出现 href,但仍执行绑定(bind)到 a 标记的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333715/