javascript - Jquery - 查找子ID的类

标签 javascript jquery

我有以下内容:

<div id=box1>
    <div id='1' class='product' style=""></div>
    <div id='2' class='product' style="display: none;"></div>
    <div id='3' class='product' style="display: none;"></div>
</div>
<div id=box2>
    <div id='4' class='product' style=""></div>
    <div id='5' class='product' style="display: none;"></div>
    <div id='6' class='product' style="display: none;"></div>
</div>
<div id=box3>
    <div id='7' class='product' style=""></div>
    <div id='8' class='product' style="display: none;"></div>
    <div id='9' class='product' style="display: none;"></div>
</div>

我想做的是专门处理特定框中的元素并忽略所有其他元素。

我知道我需要使用子元素,这就是我目前拥有的:

 $( "some-click-event" ).click(function() {
     var boxid = $(this).data("boxid"); //taken from the clicked element

        //Now I want to work exclusively with the particular box

        $("#" + boxid).children(function() {
            //Now I need to recurse through each box element  
            $( ".product" ).each(function( index ) {
                //Toggle display
            });
        });

    });

所以我的问题是:如何定位特定的盒子 ID?

最佳答案

您可以使用find方法:

$("#box" + boxid).find( '.product' ).each(function() {
    //Toggle display 
    $( this ).toggle();
});

关于javascript - Jquery - 查找子ID的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27709026/

相关文章:

javascript - Createjs - Tweenjs 不适用于位图

javascript - 在 Facebook 应用程序中自动完成的替代方法

javascript - 合并流并行与串行

javascript - JQUERY 需要帮助!

javascript - 如何根据url设置jquery ajax headers授权?

javascript - 将 cookie 中的值存储到 JavaScript 变量

javascript - ES6 对象中的方法 : using arrow functions

javascript - 为什么人们必须在同一个页面上使用多个版本的 jQuery?

javascript - 更改当前页面菜单项的样式

javascript - jQuery 中的日期选择器大小无法调整