有没有办法确定一个 <select>下拉菜单打开? [英] Is there a way to determine if a <select> dropdown menu is open?

查看:20
本文介绍了有没有办法确定一个 <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.

对于我自己的使用,我只是使用 onfocusonblur 来跟踪选择是否具有焦点.我假设没有焦点就无法打开菜单,这似乎适用于我测试过的所有浏览器.它实际上并没有告诉我菜单什么时候打开,但它告诉我什么时候不能打开,这对我的目的来说已经足够了.

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.

推荐答案

您可以在 select 的 option 子元素中使用 mouseenter 事件,因为您只能在以下情况下输入它们选择菜单打开,或者也有select元素上的click事件,通常在打开时抛出.

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天全站免登陆