0

我有以下列表,并想从中删除一个元素。在这种特殊情况下,我想删除第二个元素:

<a id="q1" rel="1" class="pagenum" href="#">2</a>

这是列表的代码:

        <div id="carousel">
        <ul class="overview">
          <li id="l1">
              <a id="q0" rel="0" class="pagenum" href="#">1</a>
              <a id="q1" rel="1" class="pagenum" href="#">2</a>
              <a id="q2" rel="2" class="pagenum" href="#">3</a>
              <a id="q3" rel="3" class="pagenum" href="#">4</a>
              <a id="q4" rel="4" class="pagenum" href="#">5</a>                
          </li>
         </ul>
        </div>

提前致谢。

编辑:我希望使用类似于 $.each 的东西:

$("carousel li").each(function(index){
   if(index == SOME_NUM){
       this.remove()
   }
 });

但是,这似乎不起作用。

附加编辑:

感谢所有回答的人并向他们道歉。我没有正确表达我的问题。在这里发布之前,我确实找到了 remove() 函数 - 它在谷歌上有很好的记录:)。但是,我试图遍历“li”的元素,然后通过 $.each 循环删除特定元素。

感谢您提供任何和所有其他回复。

4

3 回答 3

4

ID是唯一的..您可以简单地使用$('#q1').remove();

根据您的评论

我试图避免使用 id ,因为它们有很多。我缩小了这个例子。我希望使用以下内容: $("overview li").each(function(index){ this.remove } 但这似乎不起作用。

尝试使用.eq函数或:eq选择器从列表中选择特定的链接标签,

$('.overview li a:eq(1)') //-> Will return you the 2nd link
于 2012-04-13T17:37:49.743 回答
2

我认为这应该做你想做的事。

$counter = 0;
$("#carousel li").each(function(){
    if($counter == SOME_NUM)
    {
        this.remove();
    }
    $counter++;
});

You'll want to use jquery $variables here rather than javascript vars because javascript vars are function scoped, while the $variables are document scoped.

于 2012-04-13T18:42:11.720 回答
1

去除的基本形式使用remove

$(selector).remove();

因此,对于第二个元素,您可以使用[id]

$('#q1').remove();

如果要删除列表中的第二项,独立于该项[id],可以使用:

$('#l1 :eq(1)').remove();

请注意,:eq使用基于 0 的索引

移除功能本身很容易使用,难点在于确定哪种选择器效果最好。

于 2012-04-13T17:38:23.703 回答