Using jquery remove how can i remove all the span tags except for the first one..
使用jquery删除如何删除除第一个之外的所有span标记..
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>
';
5 个解决方案
#1
74
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();
#2
26
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.slice()给定一个表示一组DOM元素的jQuery对象,.slice()方法构造一个新的jQuery对象,该对象包含由start和(可选)end参数指定的元素的子集。
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.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()将选择并删除第一个实例后的所有元素。
#3
8
Use this selector:
使用此选择器:
$('span:not(first-child)')
So your code is this:
所以你的代码是这样的:
$('span:not(first-child)').remove();
#4
4
Try this
尝试这个
$('html').not(':first').remove();
#5
4
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
#1
74
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();
#2
26
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.slice()给定一个表示一组DOM元素的jQuery对象,.slice()方法构造一个新的jQuery对象,该对象包含由start和(可选)end参数指定的元素的子集。
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.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()将选择并删除第一个实例后的所有元素。
#3
8
Use this selector:
使用此选择器:
$('span:not(first-child)')
So your code is this:
所以你的代码是这样的:
$('span:not(first-child)').remove();
#4
4
Try this
尝试这个
$('html').not(':first').remove();
#5
4
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