jQuery 选择器问题
编辑:由于以下解决方案不起作用,我添加了更多代码以使其清晰。
我正在动态生成以下 html。它基本上是一个定位的跨度,以便每当单击 ProductControl 时,productMenuHolder div 都会弹出一个菜单。鼠标离开后,菜单消失。
<span class="productControl">
<div class="productMenuHolder" style="display:none;">
<ul class="productMenuList">
<li class="productMenuItem">Menu Item 1</li>
<li class="productMenuItem">Menu Item 2</li>
<li class="productMenuItem">Menu Item 3</li>
</ul>
</div>
</span>
下面是弹出菜单并在鼠标离开菜单区域时隐藏菜单的 jQuery 示例。
//shows the hover image
$(".productControl").live('hover',function (){$(this).toggleClass("productControl_hover");});
//pops the menu when productControl is clicked
$(".productControl").live('click',function(){$(this).find('.productMenuHolder').show();});
//hides the menu when the mouse leaves the menu
$('.productMenuHolder').live('mouseleave',function() {$(this).hide();});
//what i want is to hide the menu when a list item is clicked, however none of the solutions (e.g. closest, find, parent) seem to hide it!
$('.productMenuHolder li').live('click',function(){
//why are none of the solutions hiding it?
$(this).closest('.productMenuList').hide();
$(this).closest('.productMenuHolder').hide();
});
EDIT: As the below solutions have not worked, I've included more code to be clear.
I am dynamically generating the following html. It basically is a span that's positioned so that whenever productControl is clicked the productMenuHolder div pops up with a menu. Upon mouseleave, the menu disappears.
<span class="productControl">
<div class="productMenuHolder" style="display:none;">
<ul class="productMenuList">
<li class="productMenuItem">Menu Item 1</li>
<li class="productMenuItem">Menu Item 2</li>
<li class="productMenuItem">Menu Item 3</li>
</ul>
</div>
</span>
Below is a sample of the jQuery that pops up the menu and hides it when the mouse leaves the menu area.
//shows the hover image
$(".productControl").live('hover',function (){$(this).toggleClass("productControl_hover");});
//pops the menu when productControl is clicked
$(".productControl").live('click',function(){$(this).find('.productMenuHolder').show();});
//hides the menu when the mouse leaves the menu
$('.productMenuHolder').live('mouseleave',function() {$(this).hide();});
//what i want is to hide the menu when a list item is clicked, however none of the solutions (e.g. closest, find, parent) seem to hide it!
$('.productMenuHolder li').live('click',function(){
//why are none of the solutions hiding it?
$(this).closest('.productMenuList').hide();
$(this).closest('.productMenuHolder').hide();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需执行
$('.box').hide();
如果这不起作用,那么您是否尝试在代码中添加警报以查看事件是否正在触发?
或者尝试两个父母,因为苹果是 ul 的孩子,然后你就进入了盒子。
它还取决于您在什么时候创建实时代码。如果您在文档加载时创建它,那么它应该可以工作。但是,如果您在创建“.box”时创建它,那么它将无法工作,因为它尚未执行。
just do
$('.box').hide();
if this doesn't work then have you tried putting in an alert in the code to see if the event is firing?
or try two parents as apple is a child of the ul and then you get to the box.
it also depends at what point your creating the live code. if you create it on document load then it should work. if however you create it when you create ".box" then it won't work as it hasn't executed.
这是最好的方法:
但是使用
parents
也应该可以工作(区别在于:它还会隐藏 DOM 树上的.box
es)。如果它不起作用,我们可能需要查看您的更多代码。This is the best way to do it:
But using
parents
should also be working (difference is: it will also hide.box
es further up the DOM tree). If it does not work, we may need to see more of your code.使用
.closest()
。或者在
ul
的所有li
中使用class="fruits"
更新的答案
使用
.stopPropagation()
阻止其父级调用其点击处理程序,即$(".productControl").live('click',function(){...});
使其再次显示...另一个更新
use
.closest()
.or make it in all
li
oful
with theclass="fruits"
updated answer
use
.stopPropagation()
to stop it's parent calling it's click handler which is$(".productControl").live('click',function(){...});
making it showing again...another update