如何获取呼叫者事件和dom元素ID [英] How to get the caller event and dom element id
本文介绍了如何获取呼叫者事件和dom元素ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script>
function Hello(){
var caller = arguments.callee.caller;
alert( caller );
}
</script>
<input type="button" id="btnHello" value="Hello" onclick="Hello()" />
如何在不传递Hello函数中任何参数的情况下,从上述Hello函数获取按钮ID
How to get the button id from the the Hello function above with out passing the any argument in the Hello function
推荐答案
事件对象自动传递给Hello函数.您需要将其作为参数接收,然后做一些跨平台的工作来获取元素.JS框架将在这里为您提供帮助.
An event object is passed to the Hello function automatically. You need to receive it as an argument and then do some cross-platform work to grab the element. A JS framework will help you out here.
function Hello(e){
var caller = e.target || e.srcElement;
console.log( caller );
}
编辑
+1以下安德鲁的评论.最好使用addEventListener和attachEvent.
EDIT
+1 Andrew's comment below. Use addEventListener and attachEvent for best practice.
这篇关于如何获取呼叫者事件和dom元素ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文