Javascript 使用href防止VML形状上的默认单击事件
我有一个VML形状,试图阻止浏览器导航到Javascript 使用href防止VML形状上的默认单击事件,javascript,html,internet-explorer,internet-explorer-8,vml,Javascript,Html,Internet Explorer,Internet Explorer 8,Vml,我有一个VML形状,试图阻止浏览器导航到href: <!DOCTYPE html> <html> <head> <style type="text/css"> #a, #b { position: absolute; top: 10px; width: 100px; height: 100px; display: block; } #a { left: 10px; background
href
:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#a, #b {
position: absolute;
top: 10px;
width: 100px;
height: 100px;
display: block;
}
#a {
left: 10px;
background: red;
}
#b { left: 120px; }
</style>
<script type="text/javascript">
(function() {
document.namespaces.add('v', 'urn:schemas-microsoft-com:vml', '#default#VML');
window.onload = function() {
var a = document.getElementById('a');
a.attachEvent('onclick', function(e) {
console.log('A');
e.returnValue = false;
return false;
});
var b = document.getElementById('b');
b.attachEvent('onclick', function(e) {
console.log('B');
e.returnValue = false;
return false;
});
};
})();
</script>
</head>
<body>
<a id="a" href="/foo"></a>
<v:rect id="b" href="/bar"><v:fill color="#0000ff" /></v:rect>
</body>
</html>
#a、 #b{
位置:绝对位置;
顶部:10px;
宽度:100px;
高度:100px;
显示:块;
}
#a{
左:10px;
背景:红色;
}
#b{左:120px;}
(函数(){
document.namespaces.add('v','urn:schemas-microsoft-com:vml','#default#vml');
window.onload=函数(){
var a=document.getElementById('a');
a、 附件('onclick',函数(e){
console.log('A');
e、 returnValue=false;
返回false;
});
var b=document.getElementById('b');
b、 附件('onclick',函数(e){
console.log('B');
e、 returnValue=false;
返回false;
});
};
})();
在IE8中运行此示例。单击链接(红色形状)可以正确地防止浏览器使用returnValue=false/returnfalse
进入/foo
但是,尝试取消
上的导航不起作用。浏览器将导航到/bar
有解决办法吗?这应该可以:
var b = document.getElementById('b');
b.attachEvent('onclick', function(e) {
e.preventDefault();
});
您也可以尝试一下
cancelBubble
(从的internetexplorer等效项
部分)@roberkules-woops忘了说cancelBubble=true
也不起作用。@TheCloudlessky:尝试通过窗口修改事件。event
而不是从事件处理程序的参数设置returnValue
@Jay-我之前也尝试过,我只是再次尝试过,但也不起作用(将window.event.returnValue=false;window.event.cancelBubble=true;
添加到b
单击处理程序中)。