单击链接子元素时如何防止 IE 中出现 href,但仍执行绑定到 a 标记的方法?
编辑:使用 bind('click') 绑定的事件似乎也会发生同样的问题。
无论我单击链接本身还是它的任何子元素,如何防止 live() 绑定链接被跟踪?
使用以下标记:
<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() 不会执行任何有用的操作。
return false 似乎阻止除 ie6 和 ie7(ie8 未测试)之外的所有浏览器跟踪该链接,但我需要 ie6/7 才能工作。
如果有帮助的话,提供给 live() 的事件似乎是 jQuery 为 live() 方法专门制作的。它缺乏常规事件对象的大部分属性,例如originalTarget。 关于 jQuery 和 live() 事件 http://api.jquery.com/live/
Edit: The same problem seems to occur with events bound with bind('click').
How do I to prevent a live()-bound link from being followed, whether I click on the link itself or any of its child elements?
With the following markup:
<a class='dynamiclink' href='file.htm'>Some text <span> more text </span></a>
And some javascript (with 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()
});
If we click the link our e.target will contain the link and e.preventDefault() will prevent the link from being followed.
However, if we click on the span tag, e.target will contain it instead and e.preventDefault() does nothing useful.
return false seem to prevent all browsers except ie6 and ie7 (ie8 not tested) from following the link, but I need ie6/7 to work.
It if helps, the event supplied to live() seems to be specially made by jQuery for the live()-method. It lacks most of the attributes of a regular event object, like originalTarget.
On jQuery and live()-events http://api.jquery.com/live/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
或:
Try:
or:
最可能的原因是在
return false;
语句之前发生无效标记的错误。仔细检查是否有错误。The most probable cause is an error occurring before the
return false;
statement, on an invalid markup. Double-check for errors.