克隆父母及其第一个+ jQuery的最后一个孩子
说我有以下标记:
$('button').click(function() {
let row = $('div.row');
let clonedRow = row.clone(true);
clonedRow.appendTo('body');
});
.row {
border: 1px solid black;
margin-bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>append</button>
<div class="row">
<div class="first">append me!</div>
<div class="item">DONT append me</div>
<div class="item">DONT append me</div>
<div class="last">append me!</div>
</div>
我可以多次使用clone()
来实现这一目标,但是我正在寻找一种可以使用尽可能少的方法/功能来完成的干净解决方案。必须在jQuery而不是香草JS中。
Say I have the following the markup:
$('button').click(function() {
let row = $('div.row');
let clonedRow = row.clone(true);
clonedRow.appendTo('body');
});
.row {
border: 1px solid black;
margin-bottom: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>append</button>
<div class="row">
<div class="first">append me!</div>
<div class="item">DONT append me</div>
<div class="item">DONT append me</div>
<div class="last">append me!</div>
</div>
I can achieve this by using clone()
multiple times, but I'm looking for a clean solution that can do it with as little methods/functions as possible. Has to be in jQuery and not vanilla JS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
remove()
方法来删除.Item
从克隆内容中的元素:You can use the
remove()
method to remove the.item
elements from the cloned content:首先,您必须选择行,然后克隆该行&amp;然后通过 remove() methot&amp;删除该内容元素。然后将其附加到身体上。
First you have to select row, then clone that row & then remove .items elements from that content by remove() method & then append that to the body.