javascript JQuery - 如何:复制 <li> 并将其放在 <ul> 的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4487724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:51:49  来源:igfitidea点击:

JQuery - How to: Duplicate <li> and put it to the end of the <ul>

javascriptjquerycopyduplicateshtml-lists

提问by Tomkay

I have an ordinary ul li. I want to attach an li to the list which is based on the first li in the list.

我有一个普通的ul li。我想将一个 li 附加到基于列表中第一个 li 的列表中。

html

html

<div id="gallery">
<ul>
<li>Point Nr 1<p>lol</p></li>
<li>Point Nr 2</li>
<li>Point Nr 3</li>
<li>Point Nr 4</li>
</ul>
</div>

pseudo javascript/jquery

伪javascript/jquery

$("#gallery ul li").first().duplicate(attachTo:"#gallery ul li")

This is what it looks like:

这是它的样子:

<div id="gallery">
<ul>
<li>Point Nr 1<p>lol</p></li>
<li>Point Nr 2</li>
<li>Point Nr 3</li>
<li>Point Nr 4</li>
<li>Point Nr 1<p>lol</p></li>
</ul>
</div

How do I do this? :)

我该怎么做呢?:)

回答by Fatih Acet

I think it should be

我认为应该是

var ul = $('#gallery ul');
ul.find('li:first').clone(true).appendTo(ul);

回答by jAndy

Should be:

应该:

var $gallery = $('#gallery');
$gallery.find('li:first').clone(true).appendTo($gallery.find('ul'));

Example: http://www.jsfiddle.net/4yUqL/22/

示例:http: //www.jsfiddle.net/4yUqL/22/

Ref.: .clone()

参考:.clone()