Javascript 以编程方式打开下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2800266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:58:34  来源:igfitidea点击:

Programmatically open a drop-down menu

javascripthtml

提问by nickf

Possible Duplicate:
How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)

可能的重复:
如何以编程方式告诉 HTML SELECT 下拉(例如,由于鼠标悬停)

Is there a way to programmatically display/open a drop down menu (<select>)?

有没有办法以编程方式显示/打开下拉菜单 ( <select>)?

回答by alex

After trying a few things, I could not get it to work. So maybe the answer might be "it is impossible". Sorry!

尝试了几件事后,我无法让它工作。所以也许答案可能是“这是不可能的”。对不起!

The only way you could probably do it is to use one of the jQuery plugins that turn a normal select element into normal elements that are styled. Then it should be trivial to set display: blockon one of the elements the plugin produces.

您可能会这样做的唯一方法是使用 jQuery 插件之一,将普通的 select 元素转换为具有样式的普通元素。然后设置display: block插件生成的元素之一应该是微不足道的。

回答by Alex Mcp

I'll often program in a series of <select>/<option>tags, but then replace it with jQuery to become an anchor than spawns a <div>when clicked, and the <div>contains a <ul>of the options. This allows for better styling, and can be quite keyboard accessible if you choose to program that in.

我经常在一系列<select>/<option>标签中编程,然后用 jQuery 替换它以成为一个锚点,而不是<div>在单击时产生 a ,并且<div>包含 a<ul>的选项。这可以实现更好的样式,并且如果您选择对其进行编程,则可以通过键盘轻松访问。

I mostly do it because my designer insists upon non-native form elements.

我这样做主要是因为我的设计师坚持使用非原生表单元素。