<ul>
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
</ul>
我有一个像上面这样的代码。我需要使用jquery each()
单独销毁
破坏方法可以是私人功能。
怎么做?
答案 0 :(得分:1)
$('li').each(function(){
$(this).remove()
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
</ul>
&#13;
答案 1 :(得分:1)
$('ul').find('li.list').remove()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="list">Coffee</li>
<li class="">Tea</li>
<li class="list">Milk</li>
<li class="list">Coffee</li>
<li class="">Tea</li>
<li class="list">Milk</li>
</ul>
&#13;
无需使用.each()
只需选择要删除的li
答案 2 :(得分:0)
这样的事情怎么样?
// With each
$('ul li').each(function(index, value){
if($(value).hasClass('list')) // Or any other filter.
{
$(value).remove();
}
});
// Alternatively without each.
$('ul li.list').remove();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
<li class="non-list">Water</li>
</ul>
&#13;
答案 3 :(得分:0)
$("li").each(function(){
alert($(this).text());
$(this).remove();
});
答案 4 :(得分:0)
如果您使用的是jQuery,则无需使用$.each()单独删除li
。
$('li')
表示li
中可用的所有html page
。
您需要使用一行
$('li').remove();
$('#first li').remove();
// To delete the li having class list
$('#second .list').remove();
// To delete the li not having class list
$('#third :not(.list)').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="first">
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
</ul>
<p> To delete the li having class list</p>
<ul id="second">
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
<li>Fruits</li>
</ul>
<p> To delete the li not having class list</p>
<ul id="third">
<li class="list">Coffee</li>
<li class="list">Tea</li>
<li class="list">Milk</li>
<li>Fruits</li>
</ul>