简体   繁体   English

如何更改IE中选择标签的这种行为

[英]how can i change this behaviour of select tag in IE

Here is my problem: 这是我的问题:

I've got a select tag with two options - "Hello" and "World" 我有一个带有两个选项的选择标签-“ Hello”和“ World”

html HTML

<select>
<option> Hello </option>
<option> World </option>
</select>

In IE when you choose an option and and it becomes the selected option the blue highlighting remains until you click somewhere else outside the select tag. 在IE中,当您选择一个选项并将其变为所选选项时,蓝色高亮显示将一直保留,直到您单击选择标记之外的其他位置为止。 (In firefox it's not that way) (在Firefox中不是那样)

SO I wrote a script removes focus from the element when an option has been selected. 所以我写了一个脚本,当选择了一个选项时,该脚本从元素上移开了焦点。

script 脚本

$('select').change(function() {
        $(this).blur();

But still one little problem stays: if i choose Hello and then gain Hello option - the focus will remain and the blue highlighting. 但是仍然存在一个小问题:如果我选择“ Hello”然后获得“ Hello”选项-焦点将保持不变,并且蓝色突出显示。 But if I choose hello and then world option -everything works.. I read that For select menus, the change event occurs when an option is selected!!!But the option has to be different from the previous selected to trigger the change event. 但是,如果我选择打招呼,然后选择世界选项,则一切正常。.我读到对于选择菜单,当选择了一个选项时,发生更改事件!!但是该选项必须与先前选择的选项不同才能触发更改事件。

Is there any way this blue highlighting not to occur even if you choose again the same option. 有没有办法即使再次选择相同的选项也不会出现这种蓝色突出显示。

You can use a click event on select options, assuming select has id select : 您可以在select选项上使用click事件,假设select的ID为select

$('option').click(function() {
    $('#select').blur();
});

DEMO: http://jsfiddle.net/DJCe7/ 演示: http : //jsfiddle.net/DJCe7/

EDIT 编辑

If options are added dynamically, then do (assuming select has id select :) 如果选项是动态添加的,则执行(假设select具有id select :)

$('#select').on('click', 'option', function() {
    $('#select').blur();
});

EDIT 2 编辑2

Get same result pressing enter: 按回车键得到相同的结果:

$('#select').keydown(function(event) {
    // Enter pressed
    if(event.keyCode == 13) {
        $('select').blur();
    }
});

DEMO: http://jsfiddle.net/DJCe7/10/ 演示: http : //jsfiddle.net/DJCe7/10/

How about blurring it on focus? 如何使焦点模糊? Not sure how this will behave in IE though. 不确定如何在IE中表现出来。

$('select').focus(function() {
   $(this).blur();
});

http://jsfiddle.net/b26f5/ http://jsfiddle.net/b26f5/

Edit: Silly me, you could actually just remove the outline with CSS. 编辑:愚蠢的我,您实际上可以只使用CSS删除轮廓。 No need to use Javascript! 无需使用Javascript!

select, select:focus, select:active { outline: none; }

http://jsfiddle.net/b26f5/1/ http://jsfiddle.net/b26f5/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM