jQuery 选择器问题

发布于 2024-09-12 02:23:49 字数 1428 浏览 2 评论 0原文

编辑:由于以下解决方案不起作用,我添加了更多代码以使其清晰。

我正在动态生成以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-09-19 02:23:50

只需执行 $('.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.

半世蒼涼 2024-09-19 02:23:50

这是最好的方法:

$('.apple').live('click', function() { 
    $(this).closest(".box").hide();
});

但是使用 parents 也应该可以工作(区别在于:它还会隐藏 DOM 树上的 .boxes)。如果它不起作用,我们可能需要查看您的更多代码。

This is the best way to do it:

$('.apple').live('click', function() { 
    $(this).closest(".box").hide();
});

But using parents should also be working (difference is: it will also hide .boxes further up the DOM tree). If it does not work, we may need to see more of your code.

泪是无色的血 2024-09-19 02:23:50

使用 .closest()

$('.apple').live('click',function(){
   $(this).closest('.box').hide();
});

或者在 ul 的所有 li 中使用 class="fruits"

$('.fruits li').live('click',function(){
   $(this).closest('.box').hide();
});

更新的答案

//pops the menu when productControl is clicked
$('.fruits li').live('click',function(e){ // pass e as parameter...
   e.stopPropagation();
   $(this).closest('.box').hide();
});

使用 .stopPropagation() 阻止其父级调用其点击处理程序,即 $(".productControl").live('click',function(){...}); 使其再次显示...


另一个更新

$(".productControl").live('click',function(e){
   if ($(e.target).is('li')) return; // <-- add this line...
   //... other codes
});

use .closest().

$('.apple').live('click',function(){
   $(this).closest('.box').hide();
});

or make it in all li of ul with the class="fruits"

$('.fruits li').live('click',function(){
   $(this).closest('.box').hide();
});

updated answer

//pops the menu when productControl is clicked
$('.fruits li').live('click',function(e){ // pass e as parameter...
   e.stopPropagation();
   $(this).closest('.box').hide();
});

use .stopPropagation() to stop it's parent calling it's click handler which is $(".productControl").live('click',function(){...}); making it showing again...


another update

$(".productControl").live('click',function(e){
   if ($(e.target).is('li')) return; // <-- add this line...
   //... other codes
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文