似乎一个元素的onBlur被“覆盖".另一个的onclick [英] Seems that onBlur of one element is "overriding" the onclick of another
问题描述
我有两个要素:
<input type="text" name="pmTo" id="pmTo" onkeyup="offerSuggs('none');" onfocus="showSelect();" onblur="hideSelect();" />
和一个动态创建的程序(这是自动建议程序的一部分):
and a dynamically created one (this is all part of an auto suggest program):
$suggString .= '<div name="'.$value.'" id="'.$value.'" class="suggMouseOver" onmouseover="highlightDivs(this);" onmouseout="blurDivs(this);" onclick="fillInput(this);">'.$value.'</div>';
好的,然后有一些事件函数分别对应于输入元素的onblur
和div元素的onclick
:
Okay, and then there are the event functions that correspond to the onblur
of the input element and then the onclick
of the div element:
function hideSelect() {
offerSuggs('checkFinal');
document.getElementById('nameSugg').style.display="none";
}
function fillInput(elemName) {
document.getElementById('pmTo').value=elemName.id;
}
如何获取onclick
以便在他们单击的元素上正确触发,而没有onblur
事件首先隐藏div,这会使onclick
变得毫无意义?但是,当文本框失去焦点时,我仍想保留建议div消失的功能.
How do I get the onclick
to trigger correctly on the element they click on without the onblur
event hiding the div first which makes the onclick
meaningless? I would still like to retain the functionality of the suggestion div disappearing when the textbox loses focus, however.
推荐答案
我认为您的答案很短,因为您的问题令人困惑.大概您有一个输入,当重点关注时,它会根据输入到输入中的字符显示一个建议列表.
I think you are short on answers because your question is confusing. Presumably you have an input that, when focussed, shows a list of suggestions based on the characters entered into the input.
如果用户使用光标选择一个项目,那么我想输入的模糊事件会在div的click事件之前触发,并且div设置为display:none. div.
If the user uses the cursor to select an item, then I suppose the blur event of the input fires before the click event of the div and the the div is set to display:none before the click fires, and hence misses the div.
解决方法是在短暂的超时后调用onblur侦听器,因此:
The fix is to call the onblur listener after a short timeout, so:
<input ... onblur="setTimeout(function(){hideSelect();}, 100);">
在许多浏览器中进行测试,您可能需要将超时设置为200ms左右.在模糊事件之后,建议消失之前是否有一个短暂的可见延迟都没关系(即,过长总比过短要好).
Test in a number of browsers, you may need to set the timeout to 200ms or so. It doesn't matter if there's a short, visible delay after the blur event before the suggestions disappear (i.e. a bit too long is better than a bit too short).
确保建议不会使页面上的任何重要内容模糊不清,否则用户可能会发现这些建议比帮助更多. :-)
Make sure the suggestions don't obscure anything important on the page or users may find them more of a hindrance than a help. :-)
这篇关于似乎一个元素的onBlur被“覆盖".另一个的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!