我想使用 jquery 删除父 div 内 12 个 div 之后的所有 div。标记就像:
<div id="dvNames" class="container grayBox float-left">
<div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_30" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_31" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_32" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_33" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_34" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_35" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_36" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_37" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_38" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_39" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_40" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_41" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_42" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_43" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_44" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_45" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_46" type="text"></div><br></div><div class="float-left margin-bottom-10 width-33-per divContainer"><div class="float-left"><input placeholder="LastName,FirstName" onchange="CheckVisitorName(this)" class="width-180 reset" name="name_47" type="text"></div><br></div>
</div>
我知道我们可以在父 div 中获取所有 div $('#dvNames div')
。通过删除喜欢$('#dvNames div').remove()
删除所有 div。我们也可以通过$('#dvNames div:first')
.
所以我结束了
for(;$('#dvNames div').length>12;) $('#dvNames div:first').remove()
它可以按我的意图工作,但我觉得必须有某种方法可以在没有循环的情况下做到这一点。
如果您知道没有循环的方法,请帮助我。