问题描述
我有一个文本框,当您在其下方输入结果时会有过滤器。我有jQuery,可以检测焦点何时离开文本框,以便隐藏结果div。但是如果点击结果我不想隐藏结果,这样我就可以使用该点击重定向到该项目的不同页面。
I have a textbox that has filter as you type results below it. I have jQuery that detects when focus leaves the textbox so it can hide the results div. BUT I don't want to hide the results if the results are click on so that I can use that click to redirect to a different page for that item.
我'使用 .has(:focus)
来检查我的结果div 或其任何子项是否具有焦点。我的问题是,就我所知,这只适用于IE。有谁知道为什么?
I'm using .has(":focus")
to check if my results div or any of its children have focus. My problem is that this only works in IE as far as I can tell. Anyone know why?
$("#TextBox").blur(function () {
if ($("#ResultsDIV").has(":focus").length == 0) { //if you click on anything other than the results
$("#ResultsDIV").hide(); //hide the results
}
});
更新:感谢Climbage我有这个工作......
UPDATE: Thanks to Climbage I've got this working...
var resultsSelected = false;
$("#ResultsDIV").hover(
function () { resultsSelected = true; },
function () { resultsSelected = false; }
);
$("#TextBox").blur(function () {
if (!resultsSelected) { //if you click on anything other than the results
$("#ResultsDIV").hide(); //hide the results
}
});
工作示例:
推荐答案
问题是DOM上的所有非元素实际上都接受焦点,并且由浏览器决定哪些元素可以做到。请参阅
The problem is that all not elements on the DOM actually accept focus, and it's up to the browser to decide which ones do. See Which HTML elements can receive focus?
可能在 #ResultsDIV
上有一个 hover
事件,它设置一个全局变量来确定是否单击它时,该元素具有焦点。
Maybe have a hover
event on your #ResultsDIV
that sets a global variable to determine if the element has focus when you click on it.
这篇关于FF / Chrome / Safari中的jQuery .has(":focus")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!