繁体   English   中英

防止IE8中的onclick事件

[英]Prevent onclick event in IE8

我似乎无法让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;
});

试试这个...了解其他条件..

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

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


});

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

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

这在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM