单击链接子元素时如何防止 IE 中出现 href,但仍执行绑定到 a 标记的方法?

发布于 2024-08-22 17:49:37 字数 944 浏览 4 评论 0原文

编辑:使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-08-29 17:49:37

尝试:

$('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;
}).

Try:

$('a.dynamiclink').live('click', function() {
   return false;
}).children().click(function() { // or .find('span').click(function() {
   return false;
});

or:

$('a.dynamiclink, a.dynamiclink li').live('click', function() {
   return false;
}).
伤感在游骋 2024-08-29 17:49:37

最可能的原因是在 return false; 语句之前发生无效标记的错误。仔细检查是否有错误。

The most probable cause is an error occurring before the return false; statement, on an invalid markup. Double-check for errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文