在div中找到ul并使用jquery将li标签附加到该ul

时间:2022-07-10 20:22:36

I want to find the ul element inside a div and append li tag inside that ul using jquery. As given below in the code a parent div with id=select_name_chosen,i want to find ul class="chosen-results and append li in that div using jquery. Please help to solve my issue. This is jquery code what i am using.

我想在div中找到ul元素,并使用jquery在li中附加li标签。如下面在代码中给出一个id = select_name_chosen的父div,我想找到ul class =“selected-results并使用jquery在该div中追加li。请帮助解决我的问题。这是我正在使用的jquery代码。

var parent = $("div#select_name_chosen");
var childUl = parent.children('.chosen-results');
childUl.append($("<li class='active-results' data-option-array-index='0'>name</li>"));



<div class="chosen-container chosen-container-multi chosen-with-drop chosen-container-active" style="width: 350px;" title="" id="select_name_chosen">
<ul class="chosen-choices">
<li class="search-field">
<input type="text" value="Select Subject" class="default" autocomplete="off" style="width: 110px;" tabindex="4">
</li>
</ul>
<div class="chosen-drop">
<ul class="chosen-results">   
</ul>
</div>
</div>

2 个解决方案

#1


1  

This should work,it appends the li elements to the ul of class='chosen-results'.

这应该有效,它将li元素附加到class ='selected-results'的ul。

$("#select_name_chosen").find("ul[class='chosen-results']").append("<li class='active-results' data-option-array-index='0'>name</li>");

Fiddle: https://jsfiddle.net/fos2Lrab/

小提琴:https://jsfiddle.net/fos2Lrab/

#2


0  

You have a couple of unnecessary calls to the jQuery object in your code.

您在代码中对jQuery对象进行了几次不必要的调用。

var parent = $("#select_name_chosen");
var childUl = parent.find('.chosen-results');
childUl.append("<li class='active-results' data-option-array-index='0'>name</li>");

Here is a fiddle.

这是一个小提琴。

#1


1  

This should work,it appends the li elements to the ul of class='chosen-results'.

这应该有效,它将li元素附加到class ='selected-results'的ul。

$("#select_name_chosen").find("ul[class='chosen-results']").append("<li class='active-results' data-option-array-index='0'>name</li>");

Fiddle: https://jsfiddle.net/fos2Lrab/

小提琴:https://jsfiddle.net/fos2Lrab/

#2


0  

You have a couple of unnecessary calls to the jQuery object in your code.

您在代码中对jQuery对象进行了几次不必要的调用。

var parent = $("#select_name_chosen");
var childUl = parent.find('.chosen-results');
childUl.append("<li class='active-results' data-option-array-index='0'>name</li>");

Here is a fiddle.

这是一个小提琴。