jquery 删除除第一个元素以外的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18874298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery remove all elements except for first one
提问by Hulk
Using jquery remove how can i remove all the span tags except for the first one..
使用 jquery remove 如何删除除第一个以外的所有跨度标签..
EDIT
EDIT
var html = var htm = $("#addin").find(".engagement_data:last-child").find(".keys_values").html();
html='
<span style="display:block;" class="k_v">
<innput type="text" class="e_keys" style="width:65px;" placeholder="key"/>
<input type="text" class="e_values" style="width:65px;" placeholder="value"/>
</span>
<span style="display:block;" class="k_v">
<input type="text" class="e_keys" style="width:65px;" placeholder="key"/>
<input type="text" class="e_values" style="width:65px;" placeholder="value"/>
</span>
';
回答by hsz
Try with:
尝试:
$(html).not(':first').remove();
or to be more specific:
或者更具体地说:
$(html).not('span:first').remove();
To remove it from DOM, instead of html
variable, use your selector:
要从 DOM 中删除它,而不是html
变量,请使用您的选择器:
$('#addin .engagement_data:last-child .keys_values').not('span:first').remove();
回答by Nigel
Or, as an alternative:
或者,作为替代:
$('span').slice(1).remove();
slice()
Given a jQuery object that represents a set of DOM elements, the .slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument.start
Type: Integer
An integer indicating the 0-based position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.
slice()
给定一个表示一组 DOM 元素的 jQuery 对象,.slice() 方法构造一个新的 jQuery 对象,其中包含由 start 和可选的 end 参数指定的元素的子集。start
类型:整数
一个整数,指示开始选择元素的从 0 开始的位置。如果为负,则表示距集合末尾的偏移量。
Source: https://api.jquery.com/slice
来源:https: //api.jquery.com/slice
Therefore, $('span').slice(1).remove()
will select, and remove, all elements after the first instance.
因此,$('span').slice(1).remove()
将选择并删除第一个实例之后的所有元素。
回答by lukas.pukenis
Use this selector:
使用这个选择器:
$('span:not(first-child)')
So your code is this:
所以你的代码是这样的:
$('span:not(first-child)').remove();
回答by Hassan Sardar
Try this
尝试这个
$('html').not(':first').remove();
回答by Jeff Lowery
The above may work for the specific example, when you have nothing else in the content except child elements of the type you're looking for. But you will run into problems with more complex markup:
以上可能适用于特定示例,当内容中除了您要查找的类型的子元素之外没有其他内容时。但是你会遇到更复杂的标记问题:
<ul id="ul-id" class="f-dropdown tiny" data-dropdown-content="">
<li>
<div id="warningGradientOuterBarG" class="barberpole">
<div id="warningGradientFrontBarG" class="warningGradientAnimationG">
<div class="warningGradientBarLineG"></div>
</div>
</div>
</li>
<li>foo</li>
<li>bar</li>
</ul>
var $ul = $('#ul-id')
$ul.not(':first') //returns nothing
$ul.find(':first') // returns first <li>
$ul.find(':not(:first)') //returns the inner divs as well as the last two li's
$('#ul-id li:not(first-child)') // this returns all li's
$('#ul-id li:not(:first)') // this works: returns last two li's
$ul.find('li').slice(1) // this also works and returns the last two li's
$ul.find('li').slice(1).remove() // and this will remove them
回答by zeynab kimia mehr
This following code works for me:
以下代码对我有用:
$(html).children().not(':first').remove();