简体   繁体   English

使用主div ID在ul中添加li

[英]add li in ul using main div ID

<div id="my_chzn" class="chzn-container chzn-container-multi chzn-container-active" style="width: 200px;" title="">

      <div style="left: 0px; width: 198px; top: 22px;" class="chzn-drop">
             <ul class="chzn-results">
                <li style="" class="active-result" id="geoRange_chzn_o_0">ABC</li>
                <li style="" class="active-result" id="geoRange_chzn_o_1">DEF</li>
                <li style="" class="active-result" id="geoRange_chzn_o_2">GHI</li>
                <li style="" class="active-result" id="geoRange_chzn_o_3">JKL</li>
                <li style="" class="active-result" id="geoRange_chzn_o_4">MNO</li>
                <li style="" class="active-result" id="geoRange_chzn_o_5">PQR</li>
             </ul>
      </div>
</div>

<div id="geoRange_chzn" class="chzn-container chzn-container-multi chzn-container-active" style="width: 200px;" title="">

      <div style="left: 0px; width: 198px; top: 22px;" class="chzn-drop">
             <ul class="chzn-results">
                <li style="" class="active-result" id="geoRange_chzn_o_0">ABC</li>
                <li style="" class="active-result" id="geoRange_chzn_o_1">DEF</li>
                <li style="" class="active-result" id="geoRange_chzn_o_2">GHI</li>
                <li style="" class="active-result" id="geoRange_chzn_o_3">JKL</li>
                <li style="" class="active-result" id="geoRange_chzn_o_4">MNO</li>
                <li style="" class="active-result" id="geoRange_chzn_o_5">PQR</li>
             </ul>
      </div>
</div>

The first main div id=my_chzn and the second main div id=geoRange_chzn and other things are same; 第一个主div id=my_chzn ,第二个主div id=geoRange_chzn和其他东西是一样的; How can I add li based upon the main div ID, in the above scenario? 在上面的场景中,如何根据主div ID添加li

You can do 你可以做

$('#my_chzn .chzn-drop ul').append('<li class="active-result" id="geoRange_chzn_o_6">STU</li>');

to add a li in the ul inside the div of class chzn-drop inside the div with id my_chzn . 在id为my_chzn的div里面的类chzn-drop的div里面的ul添加一个li

To add the li to the other div, change the id like this : 要将li添加到其他div,请更改id,如下所示:

$('#geoRange_chzn .chzn-drop ul').append('<li/>');
var s = "geoRange_chzn" // here is the id only
$("#"+s+" *").each( function(e) {
    var s = $(this).prop("tagName");
    if(s=="UL") {
        $(this).append('<li style="" class="active-result" id="geoRange_chzn_o_5">PQR</li>');
    }
});

​ if you have only id then you can use above technique to append to UL element. 如果你只有id,那么你可以使用上面的技术附加到UL元素。

Working Fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM