0

我有一个如下的 HTML 布局。

            <div class="container">
                // Some contents are here in this level
                <div class="sub-container">
                    // Some contents are here in this level
                    <div class="sub-sub-container">     
                    // Some contents are here in this level
                    ......
                        .....
                            .....
                            .....
                        .....
                    ......
                    </div>
                </div>  
            </div>


        <div class="dynamically_added"> 
            <div class="container">
                // Some contents are here in this level
                <div class="sub-container">
                    // Some contents are here in this level
                    <div class="sub-sub-container">     
                    // Some contents are here in this level
                    ......
                        .....
                            .....
                            .....
                        .....
                    ......
                    </div>
                </div>  
            </div>
        </div>

问题是,当我尝试更新子容器或子子容器(只是嵌套在主(原始)容器 div 深处的任何 div)div 时,它也在更新动态添加的容器的内容。我怎样才能防止它,以便只更新原始内容?

4

4 回答 4

1

如果将额外的类添加"static"到顶部的静态容器中:

<div class="static container">
    // Some contents are here in this level
    <div class="sub-container">
        // Some contents are here in this level
        <div class="sub-sub-container">     
            // Some contents are here in this level
        </div>
    </div>  
</div>

你可以这样做:

$sub_container = $('.static .sub-container');
于 2012-09-12T14:56:24.503 回答
1

使用 jQuery:eq()选择器

jQuery(function(){
    jQuery(".sub-container:eq(0)").html("content for first div"); // for first div
    jQuery(".sub-container:eq(1)").html("content for second div"); // for second div
})
于 2012-09-12T13:35:12.550 回答
1

您可以使用.filter()排除div内部的 s .dynamically_added

var $div = $(".sub-sub-container").filter(function () {
   return $(this).closest(".dynamically_added").length == 0;
});

因此,在您的示例中,这将过滤掉与 classdiv中的选择器匹配的所有内容,并包括所有其他内容。div.dynamically_added

演示

于 2012-09-12T13:38:08.510 回答
1
var container = $('div[class!="dynamically_added"] > .container');

应该给你每个元素都有一个container不直接嵌套在一个类下div的类dynamically_added

于 2012-09-12T13:44:55.027 回答