jQuery - 使用父ID在类中选择子项.

Bob*_*ing 9 jquery jquery-selectors

我正在使用MCustomScrollBar ,但页面上有多个滚动条实例.我需要能够分别动态地.mCSB_container<li>元素附加到每个滚动条.

本质上是同一个问题,但是接受的答案是行不通的,因为它试图使用attr来获取id,其中类没有id.

<div id="titles" class="scroll-box" style="opacity: 1;">

    <h2>Latest Titles</h2>
    <ul class="scroll-horizontal mCustomScrollbar _mCS_1">
        <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_1" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
            <div class="mCSB_container" style="position: relative; left: 0px; width: 2860px;">
                <li class="product-car"> My Car</li>
                <li class="product-bike"> My Bike </li>
                <li class="product-tree"> My Tree </li>
            </div>
        </div> 
    </ul>

</div>

<div id="titles" class="scroll-box" style="opacity: 1;">

    <h2>Latest Houses</h2>
    <ul class="scroll-horizontal mCustomScrollbar _mCS_2">
        <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_2" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
            <div class="mCSB_container" style="position: relative; left: 0px; width: 2860px;">
                <li class="product-house"> My House</li>
                <li class="product-boat"> My Boat </li>
                <li class="product-tree"> My Tree </li>
            </div>
        </div> 
    </ul>

</div>
Run Code Online (Sandbox Code Playgroud)

<div id="titles" class="scroll-box" style="opacity: 1;">

    <h2>Latest Titles</h2>
    <ul class="scroll-horizontal mCustomScrollbar _mCS_1">
        <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_1" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
            <div class="mCSB_container" style="position: relative; left: 0px; width: 2860px;">
                <li class="product-car"> My Car</li>
                <li class="product-bike"> My Bike </li>
                <li class="product-tree"> My Tree </li>
            </div>
        </div> 
    </ul>

</div>

<div id="titles" class="scroll-box" style="opacity: 1;">

    <h2>Latest Houses</h2>
    <ul class="scroll-horizontal mCustomScrollbar _mCS_2">
        <div class="mCustomScrollBox mCSB_horizontal" id="mCSB_2" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
            <div class="mCSB_container" style="position: relative; left: 0px; width: 2860px;">
                <li class="product-house"> My House</li>
                <li class="product-boat"> My Boat </li>
                <li class="product-tree"> My Tree </li>
            </div>
        </div> 
    </ul>

</div>
Run Code Online (Sandbox Code Playgroud)

所以我试过玩jQuery的孩子,但无济于事.

Nel*_*son 13

要附加到第一个滚动条,请按ID找到它,#mCSB_1然后使用子选择器 >转到所需的容器,如下所示:

$('#mCSB_1 > .mCSB_container').append('<li class="product-tree"> New LI </li>');
Run Code Online (Sandbox Code Playgroud)

并为您的第二个滚动条执行相同但ID为#mCSB_2:

$('#mCSB_2 > .mCSB_container').append('<li class="product-tree"> New LI </li>');
Run Code Online (Sandbox Code Playgroud)