当手风琴失去焦点时如何折叠它?
我正在使用手风琴,它是 jquery-ui 框架的一部分。我希望当用户单击屏幕的另一部分时,手风琴完全折叠。
我尝试将模糊事件附加到手风琴容器,但这似乎永远不会触发意外的时间。
这是 JSFiddle 上的示例
如您所见,关闭手风琴部分时会触发模糊事件并且没有其他时间。我希望仅当打开的手风琴部分失去焦点时才触发事件。
我还尝试将模糊事件附加到手风琴部分,但这似乎从未触发。 示例
I am using the accordion that is part of the jquery-ui framework. I want the accordion to collapse completely when the user clicks on another part of the screen.
I have tried to attach a blur event to the accordion container but this never seems to fire an unexpected times.
Here is an example on JSFiddle
As you can see, the blur event is fired when an accordion section is closed and at no other time. I want an event to fire only when an open accordion section loses focus.
I have also tried attaching a blur event to the accordion sections but this never seems to fire. Example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来不像你想象的那么容易。请参阅此部分解决方案:
它会破坏并重新激活手风琴,这是我目前找到的唯一解决方案。
它有效,但关闭动画无效。也许稍微尝试一下就可以让它正常工作(请参阅代码中的提示)。
编辑:
注意我删除了
-标签,因此标题如下所示:
This looks like it's not as easy as you might think. See this for a partial solution:
It destroys and reactivates the accordion, the only solution I found right now.
It works, but the closing animation does not. Maybe with a little bit trying you can get it to work properly (see the hint in the code).
EDIT:
note I removed the
<a>
-tags, so the headers look like this:我的第二次尝试,没有插件(但仍然有点重):
简单地将鼠标位置与容器的尺寸进行比较。
编辑:现在也有了焦点。
请参阅 jsfiddle
编辑 2:不幸的是,有一点但有问题,折叠的项目有时不会再次打开......必须在容器外再次单击。不知道为什么。
my second try, without a plugin (but still somewhat heavy):
Simply compares mouse position with container's dimensions.
EDIT: Now with focus, too.
See jsfiddle
EDIT 2: Unfortunately a little but buggy, collapsed items sometimes do not open again... you have to click a second time outside the container. Don't know why.
我成功地使用事件外部插件成功地做到了这一点。我认为这是一个相当沉重的解决方案,所以如果有人有更好的解决方案,那就太好了。
不使用模糊事件,只需使用绑定到容器元素的“clickoutside”事件:
如果用户在元素外部进行选项卡或在没有单击的情况下失去焦点,则这将不起作用,但这对于我的目的来说已经足够了。
I managed to do this successfully using the events outside plugin. I think this is quite a heavy solution so if anybody has a better one, that would be great.
Instead of using a blur event, just use a 'clickoutside' event binded to the container element:
This won't work if the user tabs outside the element or otherwise loses focus without clicking but it was sufficient for my purposes.
您可以使用 JQuery 的
mouseleave
事件从文档中:
这是一个 演示
HTML
JS
You can use the JQuery's
mouseleave
eventFrom the documentation:
Here's a demo
HTML
JS