使用 jQuery 从 SPAN 获取所有 HTML


get all html from span with jquery

<span class="get" attr-one="1" attr-two="2">text1</span>
<span class="get" attr-one="21" attr-two="2">text2</span>
<span class="get" attr-one="31" attr-four="2">text3</span>
<span class="get" attr-one="4" attr-three="2">tex4t</span>
<span class="get" attr-one="15" attr-five="2">text5</span>
<div id="container" style="width: 200px; height: 200px; background-color: red"></div>
$('.get').click(function(){
   $('#container').append($(this).html());
})

如何获取带有 HTML 添加的当前对象,将其附加到其他容器?在我的示例中,这仅从我的跨度中获取内容。我想得到所有的跨度与阿特里布斯等。

斯菲德尔

$('.get').click(function(){
   $('#container').append($(this).clone());// to append html like with span now it will copy 
})

$('.get').click(function(){
   $('#container').append($(this));// to append  span now it will move   
})

查看演示

尝试

$('.get').click(function(){
   $('#container').append(this.outerHTML);
})

演示:小提琴

$('.get').click(function(){
   $('#container').append($(this));
})

我正在使用 get(0( 来检索自然 dom 元素

演示

$('.get').click(function(){
   $('#container').append($(this).get(0));
})