通过“this.value"to addEventListener功能
Pass "this.value" to addEventListener Function
我正在开发一个Chrome插件,情况就像我在选项页中有几个这样的图像。
<img src="some.jpg" class="same" id="231">
<img src="some.jpg" class="same" id="234">
<img src="some.jpg" class="same" id="1324">
<img src="some.jpg" class="same" id="123">
我这样做是为了在上面的图像上为"onclick"事件添加Eventlistener
var a = document.getElementsByClassName("same");
for (var i = 0;i<=a.length;i++)
{
a[i].addEventListener('click', test);
}
所以,我想当有人点击任何图像时我想将被点击图像的id (this.id)传递给任何函数在这个例子中是test()
提前致谢
在事件处理函数test
中,您可以简单地使用this.id
访问所单击图像的ID。
如果你的意思是你想调用函数test
与点击图像的ID,这将为您工作:
a[i].addEventListener('click', function () {
test(this.id);
});
参考:https://developer.mozilla.org/en-US/docs/DOM/EventTarget.addEventListener
相关文章:
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 主体上的addEventListener('mousemove',..)-有多糟糕
- jQuery AJAX write to XML
- grep in JQuery to C#
- how to convert html <div> to pdf
- AddEventListener适用于浏览器,但不适用于Android
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Node.js - POST to iFrame?
- SVG xml to image
- addEventListener on NodeList
- Advantages to DOMParser vs template & innerHTML
- how to split a string with ','
- How to declare a Map containing certain properties with flow
- Php variable obj to js
- 如何在submit to sightly时将表单输入值作为参数传递给WCMUsePojo
- setattribute to addEventListener
- The best way to have an addEventListener on IE < 9
- 通过“this.value"to addEventListener功能
- addEventListener('exit') to inappbrowser in PhoneGap
- ES6类:access to 'this'& # 39; addeventlistener # 39;应