开发者

Access the clicked element

开发者 https://www.devze.com 2023-01-12 08:11 出处:网络
I have an element like this: <p id=\'foo\' onclick=\'go()\'>hi</p> function go() { } how do I get access to the element that generated the click (the p 开发者_Python百科element in this

I have an element like this:

<p id='foo' onclick='go()'>hi</p>

function go() {
}

how do I get access to the element that generated the click (the p 开发者_Python百科element in this case)? Do I just do:

function go() {
    var clicked = this;
}

Thanks


Like this:

<p id='foo' onclick='go(this)'>hi</p>

function go(elem) {
}


You could pass this as an argument:

<p id='foo' onclick='go(this);'>hi</p>

function go(clicked) {
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号