javascript - Remove all other list items except the one that is clicked using jQuery - Stack Overflow

I have list of this kind<ul><li><div class="pname">Name1<div><di

I have list of this kind

<ul>
<li><div class="pname">Name1</div><div class="pid">ID1</div>...</li>
<li><div class="pname">Name2</div><div class="pid">ID2</div>...</li>
<li><div class="pname">Name3</div><div class="pid">ID3</div>...</li>
...
</ul>

If I click on any of the list item, rest all other list items should be removed. Can anyone suggest how I could do this?

I have list of this kind

<ul>
<li><div class="pname">Name1</div><div class="pid">ID1</div>...</li>
<li><div class="pname">Name2</div><div class="pid">ID2</div>...</li>
<li><div class="pname">Name3</div><div class="pid">ID3</div>...</li>
...
</ul>

If I click on any of the list item, rest all other list items should be removed. Can anyone suggest how I could do this?

Share Improve this question asked Jun 26, 2012 at 21:53 KrishhKrishh 4,2316 gold badges43 silver badges52 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Assuming this is the clicked li element:

$(this).siblings('li').not(this).remove();

If the click handler is bound to something inside the list item:

$(this).closest('li').siblings('li').not($(this).parents()).remove();

You can simply replace the ul contents with the clicked item:

$('ul li').click(function(ev){
    $(this).closest('ul').html($(this));
});​

See demo

jQuery solution...

$('li').click(function() {
    $(this).siblings().remove();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770232a4592714.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信