chrome 中未委托 click 事件
在我的应用程序中,我创建了一些新元素并根据用户的操作将它们附加到页面,
var dv=document.createElement('div');
//set styles for the dv
.....
创建此 div 元素后,我将向其添加一些事件:
MyLib.Event.addDomListener(dv,'click',function(e){
console.info('click');
});
MyLib.Event.addDomListener(dv,'mousedown',function(e){
console.info('mousedown');
});
这是页面中生成的结果(一个 div 包含一个 img 标签和一个span 标签):
页面加载后,出现问题。
如果我直接单击图像,则不会触发 click
事件,但会触发 mousedown
事件。
如果我直接单击没有图像的 div,一切都会顺利。
看来,当我单击图像时,click
事件未委托给 div。
但是然后我通过 Chrome 开发工具复制最终生成的代码,它是这样的:
<div id="con" style="position: absolute; border:1px solid red; left: 10px; top: 10px; width: 20px; height: 34px; ">
<img src="http://localhost/MAPALINE/IMAGES/MARKER_LARGE.PNG" id="ov" style="left: 0px; top: 0px; position: relative; width: 20px; height: 34px; ">
<span style="text-align: center; left: 0px; top: 0px; width: 20px; position: absolute; ">1</span>
</div>
然后我将其复制到一个新的 .html 文件,并添加事件脚本:
<script type="text/javascript">
var con=document.getElementById('con');
con.addEventListener('click',function(e){
console.info('click');
},false);
con.addEventListener('mousedown',function(e){
console.info('mousedown');
},false);
</script>
现在,无论我在里面单击哪里,这两个事件都会发生该分区。
我真的是疯了,到底有什么问题呢?我没有为生成的 div 做任何特别的事情。
所以我想知道大家有没有遇到过这个问题?或者什么情况下会出现这种异常?
更新:
Mylib.Event.addDomListener=function(target,evt,handler){
if(target.addListener){
target.addListener(evt,handler,false);
}else if(target.atttchEvent){
target.attachEvent('on'+evt,handler);
}else
#~ target['on'+evt]=handler;
}
它仅适用于跨浏览器使用,并且请注意 mousedown
事件无论如何都运行良好。
In my application I create some new elements and append them to the page according to use's operation,
var dv=document.createElement('div');
//set styles for the dv
.....
After create this div element, I will add some event to it:
MyLib.Event.addDomListener(dv,'click',function(e){
console.info('click');
});
MyLib.Event.addDomListener(dv,'mousedown',function(e){
console.info('mousedown');
});
This is the generated result in the page (a div contain a img tag and a span tag):
After the page loaded, things get wrong.
If I click the image directly, the click
event does not trigger, but the mousedown
event does.
If I click the div directly without the image, everything goes well.
It seems that, when I click the image,the click
event is not delegated to the div.
But then I copy the final generated code through Chrome develop tool, it is something like this:
<div id="con" style="position: absolute; border:1px solid red; left: 10px; top: 10px; width: 20px; height: 34px; ">
<img src="http://localhost/MAPALINE/IMAGES/MARKER_LARGE.PNG" id="ov" style="left: 0px; top: 0px; position: relative; width: 20px; height: 34px; ">
<span style="text-align: center; left: 0px; top: 0px; width: 20px; position: absolute; ">1</span>
</div>
Then I copy it to a new .html file, and add the event script:
<script type="text/javascript">
var con=document.getElementById('con');
con.addEventListener('click',function(e){
console.info('click');
},false);
con.addEventListener('mousedown',function(e){
console.info('mousedown');
},false);
</script>
Now, both of the two event occur no matter where I click inside the div.
I am really crazy, what is the problem? I do not do anything specially for the generated div.
So I want to know if any of you have meet this problem? Or In which case this kind of exception can happen?
Update:
Mylib.Event.addDomListener=function(target,evt,handler){
if(target.addListener){
target.addListener(evt,handler,false);
}else if(target.atttchEvent){
target.attachEvent('on'+evt,handler);
}else
#~ target['on'+evt]=handler;
}
It just for cross-browser use,and notice that the mousedown
event works well anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到您使用两种不同的方法在两个示例之间附加侦听器。
通过使事情尽可能紧密地重复来隔离可能的原因始终是最佳实践。尝试坚持使用相同的方法来附加侦听器进行这两个测试,这可能会让您获得一些见解。
另外,我不知道“MyLib”是什么,但如果它是某种 JS 库,则问题可能出在该库中。尝试使用原生 JS 方法或不同的库。
I notice that you are using two different methods of attaching the listener between your two examples.
It is always best practice to isolate the possible causes by making things as closely duplicated as possible. Try sticking to the same method of attaching the listener for both tests and this might lead you to some insight.
Also, I don't know what "MyLib" is, but if it is some sort of JS library, the problem could be in that library. Try using the native JS methods or a different library.