javascript 获取事件的发送者?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17863095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the sender of an event?
提问by Christian Graf
How can I get the sender of an onSubmit event in anybrowser? Or at least in FF and IE?
Esp. as event.srcElement
in IE seems to be the target?
Well, isn't there anything like explicitOriginaltarget
in browsers other than FF?
如何在任何浏览器中获取 onSubmit 事件的发送者?或者至少在 FF 和 IE 中?特别是 因为event.srcElement
在 IE 中似乎是目标?好吧,explicitOriginaltarget
除了FF之外的浏览器中没有类似的东西吗?
I'm looking for any solution in purejavascript, no JQuery.
我正在寻找纯javascript 中的任何解决方案,没有 JQuery。
What I want to do:
I've got a form. And depending on the sender I want to do differnt actions in the onSubmit(event)
routine.
我想做的是:我有一个表格。根据发件人的不同,我想在onSubmit(event)
例程中执行不同的操作。
What I got in my init function:
我在 init 函数中得到了什么:
var top_most_form = document.getElementById("upper_menu_form");
top_most_form.onsubmit=function(event){
var target = <apparently magical things have to happen here>;
if ("upper_menu_form" == target.id) {
AddSpinner();
AddStarttimeToForm();
AddThruputToUpperForm();
} else {
return false;
}
回答by Wood
Here you have a function. You just need to evaluate wich parameter is present in your event object
在这里你有一个功能。您只需要评估事件对象中存在的参数
function getTarget(e){
e=e||window.event;
return (e.target||e.srcElement);
};