如何判断我点击哪个元素来触发模糊事件处理程序? [英] How can tell I which element i clicked on to trigger a blur event handler?
问题描述
我尝试过document.activeElement,但我得到一个HTMLBodyElement,而不是我单击的元素。
code:
$ j(。input_filtrare_camp)。blur(
function(event){
nr_img = this.parentNode.id.split(_)[1];
//alert(document.activeElement);
if(document.activeElement.id.indexOf(img_exact)< 0)//仅当我点击的元素的id不运行时包含img_exact
enter_input_filtrare(event.target);
});
警报当然是用于故障排除的。
我使用了techfoobar的解决方案,如下所示:
var currElem = null ;
$(document).mousedown(function(e){
currElem = e.target;
});
$ j(。input_filtrare_camp)。blur(
function(event){
nr_img = this.parentNode.id.split(_)[1];
if(currElem.id.indexOf(img_exact)< 0)//仅当我点击的元素的id不包含img_exact时才运行
enter_input_filtrare(event.target) ;
});
查看PointedEars的答案以获取关于此问题的一些重要信息。
检查此小提琴:
该解决方案将文档的mousedown中的活动元素设置为触发文档中的所有元素,并用它来确定是否在模糊事件中执行代码。
CODE
var currElem = null;
$(document).mousedown(function(e){
currElem = e.target;
});
$ b $('#f1')。blur(function(){
if(currElem!= null&& currElem.id!=f3){
$(this).val('F1:点击elem不是f3,blur event worked');
//在这里做你的模糊的东西
}
else {
// ('F1:点击elem是f3,没有模糊事件');
}
});
Quite simple. I have a blur() event handler that fires a function, I want the function not to fire when the blur is triggered by the click on a certain element.
I tried document.activeElement but i get a HTMLBodyElement instead of the element I click on.
code:
$j(".input_filtrare_camp").blur(
function(event) {
nr_img = this.parentNode.id.split("_")[1];
//alert(document.activeElement);
if(document.activeElement.id.indexOf("img_exact") < 0) //run only if the id of the element I clicked on doesn't contain "img_exact"
enter_input_filtrare(event.target);
});
the alert is for troubleshooting, of course.
I used techfoobar's solution as follows:
var currElem = null;
$(document).mousedown(function(e) {
currElem = e.target;
});
$j(".input_filtrare_camp").blur(
function(event) {
nr_img = this.parentNode.id.split("_")[1];
if(currElem.id.indexOf("img_exact") < 0) //run only if the id of the element I clicked on doesn't contain "img_exact"
enter_input_filtrare(event.target);
});
Check out PointedEars' answer for some important information regarding this issue.
Check this fiddle:
The solution sets the active element on document's mousedown which is triggered for all elements in the document and uses that to determine whether or not to executed the code in blur event.
CODE
var currElem = null;
$(document).mousedown(function(e) {
currElem = e.target;
});
$('#f1').blur(function() {
if(currElem != null && currElem.id != "f3") {
$(this).val('F1: clicked elem is not f3, blur event worked');
// do your blur stuff here
}
else {
// for demo only, comment this part out
$(this).val('F1: clicked elem is f3, no blur event');
}
});
这篇关于如何判断我点击哪个元素来触发模糊事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!