【问题标题】:How to bring focus on an option in multiple select box如何将焦点放在多个选择框中的选项上
【发布时间】:2026-02-18 16:00:02
【问题描述】:

我想将焦点放在多选框中的一个选项上。我只想在多选框中的一个选项上添加虚线边框线(click here to refer image),并且不希望该选项被完全选中。

我尝试了类似 document.getElementById("multiple_dropdown").options[2].focus();但没用。

有什么想法吗?

谢谢。

【问题讨论】:

  • 你能用jQuery还是只用纯Javascript?
  • 没有附加图片?
  • 现在添加了图片的链接,对此感到抱歉。
  • 我也可以用jquery。

标签: javascript css focus


【解决方案1】:

经过更多调查,我发现不值得花时间尝试更改不同浏览器的默认行为,即它们如何关注多选框中的选项,原因是浏览器提供的选项不多访问多个下拉列表中的

Browser | What happens on focus of multiple dropdown after reset ?                     | Is there provision to style/focus a particular option in multiple dropdown ?
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FF      | Dotted outline border on the very first option in the multiple dropdown      | We can’t set focus on a particular option, but will be able to style particular option like a dotted blue border or background etc.
IE      | Dotted outline border on the lastly selected option in the multiple dropdown | Doesn’t provide way to style/focus particular option in a multiple dropdown
Chrome  | No dotted outline on the option but a Solid thick blue border on the whole multiple dropdown itself | Doesn’t provide way to style/focus particular option in a multiple dropdown
Mac     | Same as chrome (as both are webkit family)                                   | Doesn’t provide way to style/focus particular option in a multiple dropdown

【讨论】:

    【解决方案2】:

    不幸的是,并非所有浏览器都支持option 元素的border css 属性。 AFAIK Firefox 可以,但这是唯一的一个。所以你不能只给它做一个边框。
    如果你想选择一些选项,你可以使用 JQuery 的

    $($("#multiple_dropdown").children()[2])
    

    这将使您可以访问<select id="multiple_dropdown"> 元素的第三个(因为选项计数从零开始)option 元素。如果你想以某种方式挑选它,我可以提供给你使用background-color,例如:

    $($("#multiple_dropdown").children()[2]).css('background-color', 'blue')
    

    【讨论】:

    • 感谢您的回复,我可以尝试使用类似: $($("#multiple_dropdown").children()[2]).css('border', '1px dotted blue')
    最近更新 更多