Chrome 选择输入点击事件替代方案(更改不能满足我的需要)
当选择一个选择选项(包括重新选择)时,我需要触发一个事件。我在 ie/FF 中使用 click,但我意识到 webkit 浏览器没有 click 事件。更改对我不起作用,因为重新选择它时我也需要它。有没有办法可以实现此功能,或者我是否需要用某种组合小部件替换选择?
I need to fire an event when a select option is selected (including reselected). I was using click in ie/FF but I realized there is no click event for webkit browsers. Change will not work for me since I need it when it is reselected as well. Is there a way I can achieve this functionality or do I need to replace the select with some sort of combo widget?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有任何一个事件会在单击选项时被所有浏览器触发。在 IE 中,单击不更改选择的选项将根本不会触发任何事件。您可以在选择上捕捉
change
以及在选项上click
尝试在尽可能多的情况下获取事件,但不会 100% 。如果您有一个下拉列表,其中单击某个选项即使已被选中也会产生效果,那么您所拥有的并不是真正的选择框。选择旨在从列表中选择一个选项,如果这不是您想要的,则不应将其标记为
相反,尝试一个充满按钮(用于操作)或链接(用于导航)的框,当您单击箭头按钮时会弹出。如果您愿意,您可以将其设计为看起来像选择。
There is no one event that will get fired by all browsers on an option click. In IE an option click that doesn't change the select will fire no events at all. You can catch
change
on the select as well asclick
on the option in an attempt to get the event in any many cases as possible, but it won't be 100%.If you have a drop-down where clicking an option has an effect even if it was already selected, what you have isn't really a select box. Selects are designed to choose one option from a list and if that's not what you want you shouldn't mark it up as a
<select>
. Try to shoehorn a bunch of actions or navigation into a select box and you'll face a wide range of usability and accessibility issues.Instead, try a box full of buttons (for actions) or links (for navigation), which pops up when you click on an arrow-button. You can style it to look like a select if you like.
您始终可以尝试使用 onmouseup 或 onfocus。
You could always try using onmouseup, or onfocus.