我有类似的事情:
<div class="the_notice">
<span class="time"> | 3:48pm</span>
To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
</div>
结果我想要的东西是:
<div class="the_notice">
<span class="time"> | 3:48pm</span>
<div class="wrap">
To <strong>2013-03-16_10-56-33</strong> By <strong>XYX</strong>
</div>
</div>
如何使用jQuery实现这一目标?请帮助我。
答案 0 :(得分:5)
如果内容始终相同,您可以执行类似
的操作$('.the_notice').contents().slice(2).wrapAll('<div class="wrap"/>');
答案 1 :(得分:0)
var temp = $('.time');
$('.time').remove();
$(".the_notice").contents().wrapAll('<div class="wrap" />');
$(".the_notice").prepend(temp);
答案 2 :(得分:0)
阅读此链接可能会对您有所帮助:What is the Most effcient way to create html elements using jquery
我的答案就是这样的答案:
var myTimeIsHere = //calculate your time here;
var x = //calculate x here.;
var y = //calculate y here.;
var newHTML = "<span class='time'>" |+ myTimeIsHere+"</span>"+
"<div class='wrap'>To <strong> "+x+" </strong>"+
"by "+y+"</div>";
$(".the_notice").html(newHTML);
答案 3 :(得分:0)
使用一行(或清晰度为3):
$('.the_notice')
.wrapInner($('<div class="wrap">'))
.prepend($('.the_notice .time').detach());