复制<李>并将它们插入它们自己之后

Copy <li> and insert them after themselves

本文关键字:插入 自己 之后 lt gt 复制      更新时间:2023-09-26

我想复制所选类的每个<ul>的内容,这样

<ul class="testowyUl">
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

将更改为

<ul class="testowyUl">
  <li>a</li>
  <li>b</li>
  <li>c</li>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

这看起来很简单,但由于某种原因,我无法使它发挥作用。问题是,我正在使用类(因为会有多个这种类型的列表),我不知道如何选择我的<ul>,然后执行clone()append()。我尝试使用$(".testowyUl").children().clone().after(".testowyUl");进行链接,但没有成功。

使用$.append()和一个函数作为参数:

$(".testowyUl").append(function(){return $(this).children().clone();});

试用appendTo

$(".testowyUl").each(function() {
    $("li",this).clone().appendTo(this);
})

DEMO