admin管理员组文章数量:1023204
If I have a some javascript in an anchor's href
attribute:
<a href="javascript:alert('hello!')">
Is there a way I can get a reference to the DOM element that was clicked when the script executes? I mean, I know I could do
<a href="javascript:alert('hello from '+document.getElementById('thisAnchor'))" id="thisAnchor">
But I was hoping for something more like
<a href="javascript:alert('hello from '+target)">
If I have a some javascript in an anchor's href
attribute:
<a href="javascript:alert('hello!')">
Is there a way I can get a reference to the DOM element that was clicked when the script executes? I mean, I know I could do
<a href="javascript:alert('hello from '+document.getElementById('thisAnchor'))" id="thisAnchor">
But I was hoping for something more like
<a href="javascript:alert('hello from '+target)">
Share
Improve this question
asked Sep 22, 2010 at 19:58
rampionrampion
89.2k49 gold badges206 silver badges320 bronze badges
3 Answers
Reset to default 3Something like this?
Example: http://jsfiddle/TTzDb/
<a href="#" onclick="alert('hello from '+this.innerHTML)">me</a>
Using onclick
, this
will refer to the element that received the event.
Move the JavaScript to the onclick="yourJavaScriptHere" attribute. Then you can use the 'this' keyword to reference your anchor. So
<a href="#" onclick="alert('hello from '+this)">some text</a>
Although, that isn't very meaning. Additionally, it is a better practice to separate your JavaScript from your HTML to build a more maintainable website.
Yes, and the answer is this
which refers to current DOM element:
<a href="javascript:alert('Hello from ' + this.tagName);">Click me</a>
EDIT:
Of course as bobince mentioned (see ments) that won't work as excepted. The correct form is:
<a href="..." onclick="alert('Hello from ' + this.tagName);">Click me</a>
If I have a some javascript in an anchor's href
attribute:
<a href="javascript:alert('hello!')">
Is there a way I can get a reference to the DOM element that was clicked when the script executes? I mean, I know I could do
<a href="javascript:alert('hello from '+document.getElementById('thisAnchor'))" id="thisAnchor">
But I was hoping for something more like
<a href="javascript:alert('hello from '+target)">
If I have a some javascript in an anchor's href
attribute:
<a href="javascript:alert('hello!')">
Is there a way I can get a reference to the DOM element that was clicked when the script executes? I mean, I know I could do
<a href="javascript:alert('hello from '+document.getElementById('thisAnchor'))" id="thisAnchor">
But I was hoping for something more like
<a href="javascript:alert('hello from '+target)">
Share
Improve this question
asked Sep 22, 2010 at 19:58
rampionrampion
89.2k49 gold badges206 silver badges320 bronze badges
3 Answers
Reset to default 3Something like this?
Example: http://jsfiddle/TTzDb/
<a href="#" onclick="alert('hello from '+this.innerHTML)">me</a>
Using onclick
, this
will refer to the element that received the event.
Move the JavaScript to the onclick="yourJavaScriptHere" attribute. Then you can use the 'this' keyword to reference your anchor. So
<a href="#" onclick="alert('hello from '+this)">some text</a>
Although, that isn't very meaning. Additionally, it is a better practice to separate your JavaScript from your HTML to build a more maintainable website.
Yes, and the answer is this
which refers to current DOM element:
<a href="javascript:alert('Hello from ' + this.tagName);">Click me</a>
EDIT:
Of course as bobince mentioned (see ments) that won't work as excepted. The correct form is:
<a href="..." onclick="alert('Hello from ' + this.tagName);">Click me</a>
本文标签: htmlGet reference to anchor39s dom element from javascriptStack Overflow
版权声明:本文标题:html - Get reference to anchor's dom element from javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745542574a2155249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论