This question already has an answer here:
这个问题在这里已有答案:
- When a 'blur' event occurs, how can I find out which element focus went *to*? 23 answers
- 当'模糊'事件发生时,我怎样才能找出哪个元素焦点*到*? 23个答案
Suppose I do this:
假设我这样做:
$(target).blur(function(e){
//do stuff
});
Is there a way to fetch the object that was clicked on in order to trigger the blur action?
有没有办法获取被点击的对象以触发模糊操作?
I tried using e.target
, but that appears to be returning the object attached to the blur action rather than the clicked object.
我尝试使用e.target,但这似乎是返回附加到模糊操作的对象而不是单击的对象。
4 个解决方案
#1
31
If I understand your question correctly, this should do it:
如果我理解你的问题,应该这样做:
$(function() {
var clicky;
$(document).mousedown(function(e) {
// The latest element clicked
clicky = $(e.target);
});
// when 'clicky == null' on blur, we know it was not caused by a click
// but maybe by pressing the tab key
$(document).mouseup(function(e) {
clicky = null;
});
$(target).blur(function(e) {
console.log(clicky);
});
});
#2
39
The trick is to wait an extra tick:
诀窍是等一个额外的滴答:
$(el).blur(function (event) {
// If we just hangout an extra tick, we'll find out which element got focus really
setTimeout(function(){
document.activeElement; // This is the element that has focus
},1);
})
#3
7
Inside an event handler, this
will be the element the event is bound to, and e.target
will be the element that triggered the event (may or not be the same as this
).
在事件处理程序内部,这将是事件绑定的元素,e.target将是触发事件的元素(可能与此相同或不同)。
You are handing a blur
event, not a click
event. So, inside your event, you will have the element that you blur
ed. If you want the click
ed element, you'd need another event to get that.
您正在处理模糊事件,而不是单击事件。所以,在你的活动中,你将拥有你模糊的元素。如果你想要点击的元素,你需要另一个事件来获得它。
blur
can be triggered by other events, such as focusing something; not just clicking on something. So, there is no way to get the element that "caused the blur".
模糊可以由其他事件触发,例如聚焦某事;不只是点击某事。所以,没有办法让元素“引起模糊”。
#4
1
Using this within blur
handler function will give you the blured
element.
在模糊处理函数中使用它将为您提供模糊元素。
$(target).blur(function(e){
var bluredElement = this; // dom element
// To make a jQuery object
var bluredElement = $(this);
});
Within blur
event you can't catch the clicked element. To get the click
ed element you need click
event. For example:
在模糊事件中,您无法捕获单击的元素。要获取单击的元素,您需要单击事件。例如:
$(element).click(function() {
var clickedElement = this;
});
And to get the focused element you can use :focus
selector like: $(':focus')
will returns you focused element in document.
要获得可以使用的焦点元素:焦点选择器如:$(':focus')将返回文档中的焦点元素。
#1
31
If I understand your question correctly, this should do it:
如果我理解你的问题,应该这样做:
$(function() {
var clicky;
$(document).mousedown(function(e) {
// The latest element clicked
clicky = $(e.target);
});
// when 'clicky == null' on blur, we know it was not caused by a click
// but maybe by pressing the tab key
$(document).mouseup(function(e) {
clicky = null;
});
$(target).blur(function(e) {
console.log(clicky);
});
});
#2
39
The trick is to wait an extra tick:
诀窍是等一个额外的滴答:
$(el).blur(function (event) {
// If we just hangout an extra tick, we'll find out which element got focus really
setTimeout(function(){
document.activeElement; // This is the element that has focus
},1);
})
#3
7
Inside an event handler, this
will be the element the event is bound to, and e.target
will be the element that triggered the event (may or not be the same as this
).
在事件处理程序内部,这将是事件绑定的元素,e.target将是触发事件的元素(可能与此相同或不同)。
You are handing a blur
event, not a click
event. So, inside your event, you will have the element that you blur
ed. If you want the click
ed element, you'd need another event to get that.
您正在处理模糊事件,而不是单击事件。所以,在你的活动中,你将拥有你模糊的元素。如果你想要点击的元素,你需要另一个事件来获得它。
blur
can be triggered by other events, such as focusing something; not just clicking on something. So, there is no way to get the element that "caused the blur".
模糊可以由其他事件触发,例如聚焦某事;不只是点击某事。所以,没有办法让元素“引起模糊”。
#4
1
Using this within blur
handler function will give you the blured
element.
在模糊处理函数中使用它将为您提供模糊元素。
$(target).blur(function(e){
var bluredElement = this; // dom element
// To make a jQuery object
var bluredElement = $(this);
});
Within blur
event you can't catch the clicked element. To get the click
ed element you need click
event. For example:
在模糊事件中,您无法捕获单击的元素。要获取单击的元素,您需要单击事件。例如:
$(element).click(function() {
var clickedElement = this;
});
And to get the focused element you can use :focus
selector like: $(':focus')
will returns you focused element in document.
要获得可以使用的焦点元素:焦点选择器如:$(':focus')将返回文档中的焦点元素。