[英]remove first li of ul in jquery
There is a loop whithin loop 有一个循环,其中循环
<?php while ($data = mysql_fetch_array($some))
{?>
<div class="pro">
<div class="right-block"> bla bla bla bla </div>
<ul class="color">
<?php while($bla = $bla)
{ ?>
<li><?php echo $bla; ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
this loop show 152 products and each product have minimum 3 to 4 colors 此循环显示152个产品,每个产品至少具有3至4种颜色
now I want to remove first li
of ul
from each product 现在我要从每个产品中删除ul
首个li
if i use like this $(".color li:first").css("display" , "none");
如果我这样使用$(".color li:first").css("display" , "none");
this hide just first li
of first ul
but i want to remove first li
from each ul
这个隐藏只是第一ul
第一li
,但我想从每个ul
删除第一li
I tried using next()
and functions like .each
. 我尝试使用next()
和.each
类的.each
。 I've also created a function and called it in the first while loop just above the ul
. 我还创建了一个函数,并在ul
上方的第一个while循环中调用了它。
Nothing works. 什么都没有。
Could you help me please ? 请问你能帮帮我吗 ?
use css3 selectors: 使用css3选择器:
$('ul > li:first-child').remove();
see this fiddle to see it work. 看到这个小提琴 ,看看它的工作原理。
note that jquery's pseudo-selector :first
projects a collection to its first item, while the css selector selects all nodes that are the first child of their respective parent (in fact css3 provides the generalisation of :nth-child(an+b)
, where a
and b
are integers such that iterating n
over non-negative integers produces the index of the target elements among all children. this is not needed for your current task but might come handy at some future development stage). 请注意,jquery的伪选择器:first
将一个集合投影到它的第一项,而css选择器选择作为其各自父级的第一个子级的所有节点(实际上css3提供了:nth-child(an+b)
的推广:nth-child(an+b)
,其中a
和b
是整数,以便在非负整数上迭代n
会在所有子代中生成目标元素的索引。这对于您当前的任务不是必需的,但在将来的某个开发阶段可能会派上用场。
thanks to @Bergi for pointing out the suitability of this explanation. 感谢@Bergi指出此解释的适用性。
you can remove like this: 您可以这样删除:
$('div.pro').each(function(){
$(this).find('ul.colors').find('li:first').remove();
});
You can match the first child of any/all jQuery searches by simply using the first-child
selector (the clue is in the name) :) 您可以通过简单地使用first-child
选择器来匹配任何/所有jQuery搜索的first-child
(线索在名称中:)
eg 例如
$('ul.color li:first-child').remove();
This basically says: "find all ul
elements with the class "color", then find any li
descendants, but only match the li
when it is a first child of its parent " 这基本上是说:“找到所有ul
元素,并带有“颜色”类,然后找到任何li
后代,但仅在li
是其父代的第一个孩子时匹配li
”
:first
on the other hand says "whatever I matched before-hand, no matter how many matches just return the first element". :first
另一方面说:“无论我事先进行了什么匹配,无论有多少匹配都返回了第一个元素”。
there are so many method like eq, nth-child selectors 有很多方法,例如eq,nth-child选择器
$('.colors li').eq(0).remove();
or 要么
$('.colors li:nth-child(1)').remove();
您可以尝试以下方法:
$(".color").find("li:first").remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.