[英]Prevent onclick event in IE8
I can not seem to get IE8 to prevent the onclick function from executing. 我似乎无法让IE8阻止onclick函数执行。 Tried numerous code variations.
尝试了许多代码变体。 Any help would be appreciated.
任何帮助,将不胜感激。 As it stands now when I click on the link I get the alert popup which should not be happening.
现在,当我点击链接时,我得到了不应该发生的警报弹出窗口。
<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;
});
just try this..removing the other conditions.. 试试这个...了解其他条件..
$('a[onclick^=openMoreProductVideos]').click(function (event) {
event.preventDefault(); //will prevent the event from occuring
});
check the jsfiddle .. jsfiddle.net/kabichill/qu7kP 检查jsfiddle .. jsfiddle.net/kabichill/qu7kP
As I understand it, stopPropagation()
and cancelBubble
are used to stop the event from going further up the chain of listeners, but will still execute the current listener. 据我了解,
stopPropagation()
和cancelBubble
用于阻止事件进入侦听器链,但仍将执行当前侦听器。 Have you tried event.preventDefault()
? 你试过
event.preventDefault()
吗?
This works fine for me in IE8+ & FF 这在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 : FYI, I got stuck with if(e.cancelBubble) for IE, it has to be if(e.cancelbubble != null) IMP:仅供参考,我在IE中遇到if(e.cancelBubble),它必须是if(e.cancelbubble!= null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.