如何在javascript中获取该元素的innerHTML?
很简单的问题。我有一个元素(标签),它有一个 onclick 来调用 JavaScript 函数。除此之外,我希望这个函数能够回显调用它的元素的innerHTML。因此,在这种情况下,atag 的innerHTML。
我该怎么做?
Pretty simple question. I have an element (a tag) which has an onclick to call a javascript function. Amoung other things, I want this function to echo the innerHTML of the element that called it. So, in this case, the innerHTML of the atag.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标记:
js:
http://jsfiddle.net/g7Bau/2/
这里需要注意的重要事项本质上回答了你的问题,事件监听器在调用时已将
this
绑定到导致事件触发的 DOM 节点。所以我们可以在函数中使用this
并且它是通用的。您可以添加另一个元素,并使用fn
绑定一个点击侦听器,并且一个函数适用于这两个元素。markup:
js:
http://jsfiddle.net/g7Bau/2/
The important thing to note here, which essentially answers your question, is that the event listener when called has binds
this
to the DOM node that caused the event to fire. So we can usethis
in the function and it's generic. You could add another element, and bind a click listener also usingfn
and the one function would work for both elements.