Blur a select after change in Internet Explorer 6

不问归期 提交于 2019-12-19 04:57:49

问题


Simple question really. This works in all but IE6:

<html>
<body>
</body>
<select id="test" onchange="blur()">
<option name="one">One</option>
<option name="two">Two</option>
<option name="three">Three</option>
</select>
</html>

Is there anyway to get this working in IE6? JavaScript and JQuery hacks allowed.

Any ideas, remarks or tips? Thanks in advance


回答1:


Under jQuery:

<script type="text/javascript">
$(function() {
    $('#test').change(function() {
        $(this).blur();
    });
});
</script>

Or you may try replacing:

With:

<select id="test" onchange="javascript:blur(this);">

UPDATE: Actually it seems that the IE6 JavaScript engine has problems with blur()

A (non-ideal) work around is:

<script type="text/javascript">
$(function() {
    $('#test').change(function() {
        $('body').focus();
    });
});
</script>

Which will lose focus on the select input field by focussing the HTML document.




回答2:


<select id="test" onchange="blur()">

You want to blur the select, not the window (which acts as the global object, so this is the same as window.blur()).

<select id="test" onchange="this.blur();">

Works in all but IE6 due to a bug. You could get around it by focusing something else:

<a href="#" id="x"></a>
<select id="test" onchange="var x= document.getElementById('x'); x.focus(); x.blur();">

BUT! Either way, don't do this. You'll break keyboard accessibility for the select (since pressing up/down immediately sends a change event). This is the same problem that faces drop-downs abused as navigation.



来源:https://stackoverflow.com/questions/1471631/blur-a-select-after-change-in-internet-explorer-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!