jQuery - 单击 LI,显示/隐藏 UL - 单击 LI:a href,继续显示 UL 并在空白窗口中打开
感谢 SO 的出色贡献者!当您开始了解 jQuery 时,它会变得更酷。 :)
所以我有一个 LI,单击时会显示/隐藏子 UL。我想做的是能够单击 LI 内的链接,打开一个空白窗口,但也不会关闭子 UL。 然而,空白窗口打开已完成
a[href^="http://"]').attr("target", "_blank");
,我不确定如何在 LI 上“return: false”,这样当空白窗口打开时它不会关闭 UL。我希望用户在关闭空白窗口时能够看到他们所在的子 UL。
如果不清楚,请告诉我。
谢谢!
Thanks to the wonderful contributors here at SO! jQuery is much cooler when you begin to understand it. :)
So I have an LI that when clicked shows/hides a child UL. What I would like to do is have the ability to click on an link inside the LI that opens a blank window, but also doesn't close the child UL. The blank window opening is done
a[href^="http://"]').attr("target", "_blank");
however, I am not sure how to "return: false" on the LI so it doesn't close the UL when the blank window opens. I want the user to be able to see the child UL they were on when they close the blank window.
If this is not clear, please let me know.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的可能是 event.stopPropagation()
这是文档: http://api. jquery.com/event.stopPropagation/
基本上,发生的情况是,当您单击
时,会触发单击事件,但由于它包含在
>
点击事件也会在其上触发。事件从子级传递到父级的过程称为事件冒泡。您可以使用 event.stopPropagation() 来停止它。
假设您有一个如下所示的 HTML 结构:
此外,我们会说您有一个 CSS 规则:
应用此 jQuery,您应该得到您正在寻找的结果:
这是一个实际演示: http://jsfiddle.net/PaxTg/
I think what you are looking for is probably event.stopPropagation()
Here's the documentation: http://api.jquery.com/event.stopPropagation/
Basically, what's happening is that when you click on the
<a>
, the click event is triggering, but since it is contained in the<li>
the click event is also triggered on it. This process of the event going from child to parent is called event bubbling. You can use event.stopPropagation() to stop it.Let's assume you have an HTML structure like so:
Further, we'll say that you have a CSS rule:
Apply this jQuery, and you should get the result you're looking for:
Here's a live demo of this in action: http://jsfiddle.net/PaxTg/
您可能需要查看事件订单,然后停止传播您的事件正确的时间。
you might want to have a look at event orders and then stop propagation of your event at the right time.