防止IE8中的onclick事件

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;
});

alert(event.stopPropagation); 显示什么?为什么你的代码需要两个事件处理程序? - Mark Schultheiss
这个链接会有所帮助,还有这个 :) - Jashwant
3个回答

1

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

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

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


});

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


0
据我所知,stopPropagation()cancelBubble用于阻止事件继续向上层监听器传递,但仍会执行当前监听器。你尝试过使用event.preventDefault()吗?


是的,那也不起作用。我认为需要先删除事件处理程序,但不确定如何在IE8中使其工作。例如,以下代码适用于ff和chrome,但不适用于IE。$('a [onClick ^ = openMoreProductVideos]').each(function(e){ this.onclick = undefined; }); - septemberbrain

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;
}

注意:我在处理IE的if(e.cancelBubble)时遇到了困难,它必须是if(e.cancelbubble != null)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接