有没有办法确定
我正在寻找一种方法来确定
我可以监听焦点/模糊、mouseup/mousedown 等事件,但我认为我无法可靠地从这些事件中找出菜单的状态。例如,mousedown 后跟 mouseup 可能意味着用户单击并拖动到某个选择并释放(在这种情况下,菜单现在关闭)或单击并释放以打开菜单(在这种情况下,菜单打开)。下拉菜单的具体行为似乎也取决于浏览器。
我知道如果我滚动自己的下拉菜单就可以做到这一点,但我更喜欢使用
有没有可靠的方法来确定下拉菜单是否打开?或者这是 Javascript 无法知道的事情?
结论:似乎没有任何有保证的方法来确定选择菜单是否打开,无论是通过询问对象还是通过侦听它触发的事件。
对于我自己的使用,我只是使用 onfocus 和 onblur 跟踪选择是否具有焦点。我假设没有焦点就无法打开菜单,这在我测试过的所有浏览器中似乎都是如此。它实际上并没有告诉我菜单何时打开,但它告诉我何时无法打开,这对于我的目的来说已经足够了。
I'm looking for a way to determine if/when a <select>
element's menu is open. I don't need to force it to open or close, just figure out if it's open or closed at a given time.
I can listen to events for focus/blur, mouseup/mousedown, etc., but I don't think I can reliably figure out the state of the menu from those events. For example, mousedown followed by mouseup could mean the user clicked and dragged to a selection and released (in which case the menu is now closed) or clicked and released to open the menu (in which case the menu is open). It also seems likely that the specific behavior of dropdown menus is browser-dependent.
I know I could do this if I roll my own dropdown menu, but I prefer to use <select>
.
Is there a reliable way to find out if a dropdown menu is open? Or is this something that Javascript can't know?
Conclusion: There doesn't seem to be any guaranteed way to determine if a select menu is open, either by asking the object or by listening to the events it fires.
For my own use I'm just keeping track of whether the select has focus using onfocus and onblur. I'm assuming there's no way for the menu to be open without having focus, and that seems to hold true in all the browsers I've tested. It doesn't actually tell me when the menu is open, but it tells me when it can't be open, which is good enough for my purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 select 的
option
子元素中使用mouseenter
事件,因为只有在 select 菜单打开时才可以在其中输入,或者也可以使用click
select
元素上的事件,通常在打开它时引发。我认为要在精确的时刻测试它是否打开是不可能的。
You can play with
mouseenter
event in select'soption
child elements as you can only enter in them if select menu is open, or also with theclick
event on theselect
element, usually thrown when you open it.To test in a precise moment if its open or not I think is not possible.
在 Internet Explorer 中,选择元素是出了名的棘手。我认为没有任何方法可以可靠地确定一个是打开还是关闭。
Select elements are notoriously tricky in Internet Explorer. I don't think there is any way of reliably determining if one is open or closed.
我可以查看选择是否打开,然后当用户选择新选项或完全模糊选择时关闭。但是,如果您单击相同的选项,或者仅单击一次选择(关闭它但不模糊它),它仍然显示“向下”。致力于制定更完整的解决方案。所以,期望这个答案有编辑...
编辑:添加了 mouseout
编辑2:添加了 mouseup - 现在可以使用!
I can see if the select is open, and then when it closes if the user selects a new option, or completely blurs the select. However, if you click on the same option, or just click out of the select once (closing it but not blurring it), it still shows "down". Working on a more complete solution. So, expect this answer to have edits...
Edit: added mouseout
Edit2: added mouseup - works now!
这是您问题的完整答案。是的,它有点长,但它在所有场景中都具有魅力。
当用户在我的实验中快速选择一个选项时,上面使用 onClick 的答案不起作用,所以我为什么不给出完整的答案
我还在媒体上写了一篇关于它的文章,如果需要更多说明,请访问。
https://medium .com/@ijlal.tanveer294/how-to-know-if-a-select-dropdown-is-open-555a304e7dda
Here is a full answer to your question. Yes its a bit long but it works like a charm in all scenarios.
Above answers which uses onClick don't work when user selects a option fast in my exp, so i though why not give a full answer
Also I wrote an article on medium about it, do visit if need more clarification.
https://medium.com/@ijlal.tanveer294/how-to-know-if-a-select-dropdown-is-open-555a304e7dda
更完整的解决方案是:
A more complete solution would be: