如何将li项附加到给定id的fisrt ul

时间:2018-03-01 06:40:13

标签: jquery html html-lists bootstrap-select bootstrap-selectpicker

我正在尝试访问给定<ul> div的2(更多)div内的第一个<div class="form-group" id="contract">,即$('#contract')

代码

<div class="form-group" id="contract">
    <div class="btn-group bootstrap-select search-fields open">
        <button type="button" class="btn dropdown-toggle bs-placeholder btn-default" data-toggle="dropdown" role="button" data-id="ct" title="Contract Types" aria-expanded="true">
            <span class="filter-option pull-left">Contract Types</span>&nbsp;
                <span class="bs-caret">
                    <span class="caret"></span>
                </span>
        </button>
        <div class="dropdown-menu open" role="combobox" style="max-height: 590px; overflow: hidden; min-height: 158px;">
            <div class="bs-searchbox">
                <input type="text" class="form-control" autocomplete="off" placeholder="Search value" role="textbox" aria-label="Search">
            </div>
            <ul class="dropdown-menu inner" role="listbox" aria-expanded="true" style="max-height: 543px; overflow-y: auto; min-height: 111px;">
                <li data-original-index="0" class="selected active">
                    <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="true">
                        <span class="text">Contract Types</span>
                        <span class="glyphicon glyphicon-ok check-mark"></span>
                    </a>
                </li>
                <li data-original-index="1">
                    <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
                        <span class="text">Rent</span>
                        <span class="glyphicon glyphicon-ok check-mark"></span>
                    </a>
                </li>
                <li data-original-index="2">
                    <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
                        <span class="text">Buy</span>
                        <span class="glyphicon glyphicon-ok check-mark"></span>
                    </a>
                </li>
                <li data-original-index="3">
                    <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
                        <span class="text">Commercial Rent</span>
                        <span class="glyphicon glyphicon-ok check-mark"></span>
                    </a>
                </li>
                <li data-original-index="4">
                    <a tabindex="0" class="" data-tokens="null" role="option" aria-disabled="false" aria-selected="false">
                        <span class="text">Commercial Buy</span>
                        <span class="glyphicon glyphicon-ok check-mark"></span>
                    </a>
                </li>

                <-- *** I want to append new li items here using $('#contract') of parent's parent's parent (3rd parent's) div *** -->
            </ul>
        </div>
        <select class="selectpicker search-fields" id="ct" name="ct" data-live-search="true" data-live-search-placeholder="Search value" tabindex="-98">
            <option value="">Contract Types</option>
            <option value="1">Rent</option>
            <option value="2">Buy</option>
            <option value="3">Commercial Rent</option>
            <option value="4">Commercial Buy</option> 
            <-- *** here, I can append a new option like *** -->
            <script>
                $('#ct').append('<option value="5">New option</option>');
            </script>                                                                                                   
        </select>
    </div>
</div>

如何使用jQuery的第一个div的id将<li>项附加到(first&amp; only)<ul>目标div不是父级,而是父级父级的父级。

2 个答案:

答案 0 :(得分:1)

您可以使用.find()在其所有子元素中查找元素,然后使用方法.append()将一些html附加到上一步返回的jquery对象。

$("#contract").find(".dropdown-menu").append('<li>li Content</li>')

答案 1 :(得分:1)

如果要将新项目添加为列表中的第一项,请使用prepend方法。否则,请使用append。

$('#contract').find('ul').prepend('<li>added node</li>')

JSFiddle

如果可能,可以在ul元素中添加css类或id,并将其用作jquery中的选择器。如果不可能,您可以使选择器更精确,以避免选择错误的节点,例如$('#contract .dropdown-menu > ul').prepend('<li>added node</li>')