admin管理员组文章数量:1240587
I have this code:
var x = document.getElementsByClassName("hex");
for(var i = 0; i < x.length; i++)
{
x[i].addEventListener("click", myFunction);
}
To attach onclick dynamically. My question is myFunction
how to get the clicked element?
I have this code:
var x = document.getElementsByClassName("hex");
for(var i = 0; i < x.length; i++)
{
x[i].addEventListener("click", myFunction);
}
To attach onclick dynamically. My question is myFunction
how to get the clicked element?
1 Answer
Reset to default 13Let myFunction
take in an argument (call it event
). event.target
is then the clicked element:
function myFunction(event) {
var clickedElement = event.target;
// Do important stuff with clickedElement.
}
本文标签: dom eventsJavaScript get the sender elementStack Overflow
版权声明:本文标题:dom events - JavaScript: get the sender element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740081928a2223545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论