是否有一种方法来确定< select>下拉菜单是否打开? [英] Is there a way to determine if a <select> dropdown menu is open?

查看:173
本文介绍了是否有一种方法来确定< select>下拉菜单是否打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来确定是否/何时< select> 元素的菜单打开。我不需要强制它打开或关闭,只是在给定的时间弄清楚它是开放还是关闭。

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.

我可以收听焦点/模糊的事件,mouseup / mousedown等,但我不认为我可以从这些事件中可靠地计算出菜单的状态。例如,mousedown后面的mouseup可能意味着用户点击并拖动到选择并释放(在这种情况下,菜单现在关闭)或单击并释放以打开菜单(在这种情况下,菜单是打开的)。下拉菜单的具体行为也似乎与浏览器相关。

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.

我知道如果我滚动自己的下拉菜单,我可以这样做,但我更喜欢使用< select>

I know I could do this if I roll my own dropdown menu, but I prefer to use <select>.

有没有可靠的方法来确定下拉菜单是否打开?或者这是JavaScript不能知道的东西?

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.

对于我自己的使用,我只是跟踪选择是否使用 onfocus onblur 。我假设菜单没有方法可以打开而没有焦点,这似乎在我测试的所有浏览器中都成立。它实际上并没有告诉我菜单是否开放,但它告诉我什么时候我不能开放,这对我的目的来说足够好。

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.

推荐答案

您可以在选择的选项中使用 mouseenter 子元素,如果选择菜单打开,或者也可以使用选择上的单击事件,您只能输入它们元素,通常在打开时抛出。

You can play with mouseenter event in select's option child elements as you can only enter in them if select menu is open, or also with the click event on the select element, usually thrown when you open it.

要在一个确切的时刻测试它是否开放,我认为是不可能的。

To test in a precise moment if its open or not I think is not possible.

这篇关于是否有一种方法来确定&lt; select&gt;下拉菜单是否打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆