3

我似乎无法让 IE8 阻止 onclick 功能执行。尝试了许多代码变体。任何帮助,将不胜感激。就目前而言,当我单击链接时,我会弹出不应发生的警报弹出窗口。

 <p><a href="#" onclick="openMoreProductVideos('Video title','videocode','false');return false;"><span class="txt">View all videos</span></a></p>

function openMoreProductVideos(title,productCode,fastprevoew) 
{  
  alert ("openMoreProductVideos - should not see this!");
}

$('a[onclick^=openMoreProductVideos]').click(function (event) {
   if (event.stopPropagation){
         event.stopPropagation();
   }
   else if(window.event){
      window.event.cancelBubble=true;       
   }       
    return false;
});
4

3 回答 3

1

试试这个..删除其他条件..

$('a[onclick^=openMoreProductVideos]').click(function (event) {

   event.preventDefault();      //will prevent the event from occuring


});

检查 jsfiddle .. jsfiddle.net/kabichill/qu7kP

于 2012-06-13T16:44:18.530 回答
0

据我了解,stopPropagation()cancelBubble用于阻止事件在侦听器链中进一步上升,但仍将执行当前侦听器。你试过event.preventDefault()吗?

于 2012-06-13T16:32:37.037 回答
0

这在 IE8+ 和 FF 中对我来说很好用

function stopEvent(e) {    
    if (!e) e = window.event;

    if (e.cancelBubble != null) e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation(); //e.stopPropagation works in Firefox.
    if (e.preventDefault) e.preventDefault();
    if (e.returnValue != null) e.returnValue = false; // http://blog.patricktresp.de/2012/02/
    return false;
}

IMP:仅供参考,我被 IE 的 if(e.cancelBubble) 卡住了,它必须是 if(e.cancelbubble != null)

于 2014-09-08T12:39:49.633 回答