尝试获取在按钮上调用的方法名称,如下所示:
<button onclick="MyFunction">Button 1</button>
<input type="button" name="hola" value ="hola"onclick="MyFunction2">我想从按钮中获取"MyFunction“或"MyFunction2”方法名
<script>
function MyFunction(){
**//sample method**
Alert("hola mundo");
}
function MyFunction2(){
**//sample method**
Alert("hola mundo2");
}
</script>发布于 2018-04-27 08:34:48
您可以使用getAttribute方法在onclick属性上检索函数的名称:
var buttonElement = document.getElementsByTagName("button")[0];
var buttonFunctionName = buttonElement.getAttribute("onclick");发布于 2018-04-27 08:33:01
假设你有一个带有“clickme”和onclick="myfunc“的按钮。
要在脚本中获取onclick函数,请使用:
document.getElementById("clickme").onclick // returns reference to myfunc
要获取onclick的方法名,请使用:
document.getElementById("clickme").onclick.name // returns "myfunc"
https://stackoverflow.com/questions/50053530
复制相似问题