addEventListener 在事件侦听器中不起作用
我有一个事件监听器,它监听文档加载(这是“myExtension”并且它工作正常)。而且我想在这个监听器中放入另一个监听器,但是不行。这是那个监听器:
myExtension.$("#quicklink img").addEventListener("click", function(){ alert("It's works!"); }, false, true);
但是如果我将该监听器更改为这个监听器:
document.addEventListener("click", function(){ alert("It's works!"); }, false, true);
它可以正常工作。
$("#quicklink img")
- 它是一个包含图像的数组,页面中有一些图像(0-20 个)。
我想使用这个,只有一些页面,所以我需要 myExtension,因为我不想工作所有网站,其中具有快速链接 id 的元素中有 img。
为什么它不起作用?问题是什么?
I have an event listener, which listen to document load (this is "myExtension" and it's work correctly). And I want to put another listener into this listener, but it doesn't work. This is that listener:
myExtension.$("#quicklink img").addEventListener("click", function(){ alert("It's works!"); }, false, true);
but if I change that listener to this listener:
document.addEventListener("click", function(){ alert("It's works!"); }, false, true);
it works correctly.
$("#quicklink img")
- it's an array with images, there are some of them in the page (0-20 ea).
I want to use this, only some pages, so I need myExtension, because I don't want to work all sites, where there are img in an element with quicklink id.
Why it doesn't work? What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了我的答案
,其中“a1”是一个图像 ID
I find my answer
where "a1" is one image id
数组没有
addEventListener
方法。您必须对其进行循环并在每个 HTMLElementNode 上调用addEventListener
。Arrays don't have an
addEventListener
method. You would have to loop over it and calladdEventListener
on each HTMLElementNode.您需要在每个图像上添加侦听器
you need to add the listener on each image