获取调用onclick函数的元素 [英] Get element from which onclick function is called
本文介绍了获取调用onclick函数的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有onclick属性的按钮,该属性指向函数 test()
。
I have a button with a onclick attribute which is pointing to the function test()
.
<button onclick="test()">Button 1</button>
<button onclick="test()">Button 2</button>
<button onclick="test()">Button 3</button>
功能测试():
function test()
{
var button_name = this.html;
console.log("Im button "+ button_name);
}
如何获得有关点击按钮的信息?
,例如我如何阅读HTML?
How can I get informations about the clicked button? e.g. How can i read the html?
jsfiddle : https://jsfiddle.net/c2sc9j9e/
推荐答案
通过这个
对该函数的引用,然后阅读 textContent
属性节点的文本内容。
Pass the this
reference to the function, then read textContent
property the text content of the node.
HTML
<button onclick="test(this)">Button 1</button>
脚本
function test(elem){
var button_name = elem.textContent;
}
这篇关于获取调用onclick函数的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文