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

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.dynamically_added

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

したがって、あなたの例では、これにより、クラス内にあるセレクターに一致するすべてのものが除外され、他のすべてが含まれます。divdiv.dynamically_added

デモ

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

のクラスcontainerで直接ネストされていないクラスを持つすべての要素を提供する必要がありますdivdynamically_added

于 2012-09-12T13:44:55.027 に答える
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 に答える