防止IE8中的onclick事件

时间:2012-06-13 16:29:38

标签: javascript jquery internet-explorer onclick

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

3 个答案:

答案 0 :(得分:1)

试试这个..恢复其他条件..

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

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


});

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这在IE8 +&amp; amp; 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:仅供参考,我坚持使用if(e.cancelBubble)用于IE,它必须是if(e.cancelbubble!= null)