HTML选择元素关闭时是否有DOM事件触发? [英] Is there a DOM event that fires when an HTML select element is closed?

查看:130
本文介绍了HTML选择元素关闭时是否有DOM事件触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个DOM事件,我可以用JavaScript来聆听当已经打开的选择元素(但没有选项被更改)时,通过点击select元素,某个地方(任何地方)



这不是选择的 blur 事件,因为选择保留焦点。同样,它不是某些其他元素或文档的焦点事件,或 mousedown



不是更改事件的选择,因为选择中没有任何选项已被更改。



我不担心旧版Internet Explorers - 只适用于符合标准的现代浏览器。我已经创建了一个JSFiddle来演示这个问题: http://jsfiddle.net/premasagar/FpfnM/


  1. 点击结果面板中的选择框

  2. 单击单击HERE(或其他任何地方)的文本,并查看是否有任何事件添加到日志中。最新的Chrome或Firefox中没有活动。

所以问题是:可以添加什么JavaScript,在点击选择框时记录事件?



(我在这里提出了类似但不同的问题:

iOS上的JavaScript:打开HTML选择元素

解决方案

不幸的是,没有一个标准的事件来知道选择框何时关闭或打开,所以你的代码将是很漂亮的住宿为不同的浏览器。也就是说,我认为可以做到这一点,而且我已经在Firefox中使用 mouseup 事件来为你工作:



http://jsfiddle.net/FpfnM/50/



在Firefox中(我假定是Chrome),您将需要仔细跟踪该选择的状态。当转义键被按下或 blur 事件发生时,您需要更新状态以将其标识为已关闭。我没有在我的演示中实现这一点,你可以看到如果你点击逃避而不是点击选择,会发生什么。



Safari中的事情更容易, mousedown 事件选择表示打开选择,任何关闭选择都由点击事件指示。 / p>

如果您发现没有这些事件触发的浏览器,您可以尝试一个额外的技巧。因为窗体小部件(如选择和文本区域)通常由操作系统呈现,而不在浏览器中,因此可能会与其进行交互,某些消息可能无法解决浏览器的事件处理程序。如果要在打开选择框时将一个覆盖屏幕的透明文本区域定位在较低的z索引处,则可以使用该文本区域来跟踪该关闭点击。它可能能够捕获浏览器DOM元素可能会遗漏的事件。



更新:
Chrome在选择中看到mousedown当打开该页面时,打开该文档时,在该页面上单击鼠标。以下是适用于Chrome的版本:



http:/ /jsfiddle.net/FpfnM/51/



同样,您需要执行一些浏览器检测来处理每个浏览器的正确行为。特别是Chrome的一个特点是,当您点击第二次关闭它时,不会触发任何事件。您可以检测到页面点击。



快速摘要

  Chrome / Safari:select.mousedown on open,document.mouseup on close 
Firefox:select.click on open,document.mouseup on close
IE8 / IE7:select。点击open,document.mouseup关闭

其他事件有很多边缘案例,将表示关闭(转义键,模糊等),但这些是处理用户单击选择框,然后单击文档区域的场景的最小值。



希望这有帮助!


I'm looking for a DOM event that I can listen to with JavaScript for when a select element that has been opened (but no options changed) is then closed by clicking off the select element, somewhere (anywhere) else on the page.

It's not the blur event of the select, because the select retains focus. Likewise, it's not the focus event of some other element or the document, or a mousedown or click on the window, document or body.

It's not the change event of the select, since no option within the select has been changed.

I'm not concerned about legacy Internet Explorers - just something to work in standards compliant modern browsers. Proprietary hacks could be worth knowing though.

I've created a JSFiddle to demonstrate the problem: http://jsfiddle.net/premasagar/FpfnM/

  1. Click on the selectbox in the "Result" panel
  2. Click on the text marked "HERE" (or anywhere else) with a single click and see if any event is added to the log. There isn't an event in the latest Chrome or Firefox.

So the question is: What JavaScript could be added, to get an event logged when clicking off the selectbox?

(I've asked a similar, but different question here:
JavaScript on iOS: opening an HTML select element)

解决方案

Unfortunately there's no standard event for knowing when a select box is closed or open, so your code is going to be pretty hairy with accommodations for different browsers. That said, I think it can be done, and I've gotten something working for you in Firefox using the mouseup event:

http://jsfiddle.net/FpfnM/50/

In Firefox (and I'm assuming Chrome), you're going to need to track the state of that select pretty carefully. When an escape key is pressed or blur event occurs, you need to update the state to identify it as closed. I haven't implemented that in my demo, and you can see what happens if you hit escape instead of clicking off the select.

Things were easier in Safari, where a mousedown event on the select signifies opening the select, and any close of the select is signified by a click event.

If you find a browser where none of these events fire, you can try one additional trick. Because form widgets like select and textarea are often rendered by the OS and not inside the browser it's possible that you could interact with them and certain messages might not get down to the browser's event handler. If you were to position a transparent textarea covering the screen at a lower z-index when the select box is open, you might be able to use it to track that close click. It may be able to capture events that a browser DOM element may miss.

Update: Chrome sees a mousedown on the select when it opens and a mouseup on the document when the page is clicked with a select open. Here's a version that works with Chrome:

http://jsfiddle.net/FpfnM/51/

Again, you'll need to do some browser detection to handle the right behavior in each one. One catch with Chrome, in particular, is that no event is fired when you click on the select a second time to close it. You can detect the page click, however.

Quick Summary:

Chrome/Safari: select.mousedown on open, document.mouseup on close
Firefox: select.click on open, document.mouseup on close
IE8/IE7: select.click on open, document.mouseup on close

There are an awful lot of edge cases for other events that will signify a close (escape keypress, blur, etc.), but these are the minimum to handle the scenario where a user clicks the select box and then clicks off into the document area.

Hope this helps!

这篇关于HTML选择元素关闭时是否有DOM事件触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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