获取元素,在 javascript 中引起更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9655582/
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
get element, caused change event in javascript
提问by DotNetter
i want to get know within onchange
event handler, which control caused change (or blur) event.
i tried to use jQuery $(":focus")
for that aim, but
我想在onchange
事件处理程序中了解哪个控件导致了更改(或模糊)事件。我试图为此$(":focus")
目的使用 jQuery ,但是
$('.some_class').change(function (e) {
console.log(e.target);
console.log($(':focus').get(0)); // always `undefined`.
/* ......... other code......... */
});
回答by Madao
$('.some_class').change(function (e) {
console.log(e.target);
console.log($(':focus').); // just remove the .get(0)
}); remove the .get(0),you can get the element which is on focus
}); 删除.get(0),你可以得到焦点上的元素
回答by Dennis
In jQuery, this
refers to the element with the event handler and event.target
, if event
is the parameter to your event handler callback will be the source of the event, if, for example, you clicked on a child element and the click bubbled up.
在 jQuery 中,this
指的是具有事件处理程序的元素 event.target
,如果event
是事件处理程序回调的参数,则将是事件的来源,例如,如果您单击子元素并且单击冒泡。
The focus event gets fired after the blur event, so there will be a short period of time during which no element has focus. If you really need this, you could set a short timeout to see if another input
has focus a couple milliseconds after the original one gets blurred.
focus 事件在 blur 事件之后被触发,所以会有很短的时间没有元素获得焦点。如果你真的需要这个,你可以设置一个短暂的超时,看看另一个input
是否在原始焦点模糊几毫秒后有焦点。