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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:53:55  来源:igfitidea点击:

Getting the sender of an event?

javascriptcross-browser

提问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.srcElementin IE seems to be the target? Well, isn't there anything like explicitOriginaltargetin 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);
};

See: jQuery - how to determine which link was clicked

请参阅:jQuery - 如何确定点击了哪个链接