我有这个 HTML
<div class="control">
<label class="">Label Here</label>
<input type="text">
<div class="ui-resizable"></div>
<div class="ui-resizable-handle"></div>
<div class="ui-resizable-handle ui-resizable-se"></div>
<div style="display: inline;" class="delete"><span class="ui-icon ui-icon-closethick">close</span> </div>
<div style="display: inline;" class="properties txtbox"><span class="ui-icon ui-icon-wrench">Properties</span></div>
</div>
我如何使用 jQuery 从此 HTML 中删除第二个第三个和第四个 Div...
您正在寻找:nth-child
: http://api.jquery.com/nth-child-selector/
它的工作原理如下:
$('.control div:nth-child(2), .control div:nth-child(3), .control div:nth-child(4)').remove();
注意:nth-child
uses 以一为基础索引,因此第一个元素的索引为 1。
更新:为了回答这个问题,OP 在评论中发布了
如果我不知道输入字段出现后会出现多少个div
任何方式来剪切或切片所有 div 或之后出现的任何元素
控制部门的第二个孩子......
答案是肯定的,为此你想要:gt:
选择器:http://api.jquery.com/gt-selector/
$('.control div:gt(1)').remove()
相对于:nth-child
, :gt
uses 零基础索引,因此第一个元素的索引为 0。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)