Get ID of element that called a function

前端 未结 7 625
梦毁少年i
梦毁少年i 2020-12-07 20:02

How can I get the ID of an element that called a JS function?

body.jpg is an image of a dog as the user points his/her mouse around the screen at different

相关标签:
7条回答
  • 2020-12-07 20:40

    For others unexpectedly getting the Window element, a common pitfall:

    <a href="javascript:myfunction(this)">click here</a>
    

    which actually scopes this to the Window object. Instead:

    <a href="javascript:nop()" onclick="myfunction(this)">click here</a>
    

    passes the a object as expected. (nop() is just any empty function.)

    0 讨论(0)
提交回复
热议问题