开发者

Clone element with class

How to clone content of the element with class and put it into itself?

Here is example. We have this:

<div class="cloneThis">one</div>
<div class="cloneThis">two</div>

The result must be like

<div class="clone开发者_如何学JAVAThis">one<span>one</span></div>
<div class="cloneThis">two<span>two</span></div>

I tried something like

$('.cloneThis', this).append('<span>'+$('.cloneThis', this).html()+'</span>');

but it returns first element of class and put it into all other. Is there any way to solve this?


$('.cloneThis', this).each(function() {
 $(this).append('<span>' + $(this).html() + '</span>');
});


.append() takes a function, like this:

$('.cloneThis').append(function(i, html) { return $('<span>').html(html); });
//or:
$('.cloneThis').append(function(i, html) { return $('<span>', { html:html }); });

You can test it out here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜