Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用href防止VML形状上的默认单击事件_Javascript_Html_Internet Explorer_Internet Explorer 8_Vml - Fatal编程技术网

Javascript 使用href防止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

我有一个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: 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
单击处理程序中)。