5

我有类似的东西:

<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 实现这一点?请帮助我。

4

4 回答 4

5

好吧,如果内容始终相同,您可以执行以下操作

$('.the_notice').contents().slice(2).wrapAll('<div class="wrap"/>');

http://jsfiddle.net/Ysq48/

于 2013-04-02T11:51:06.903 回答
0

演示

   var temp = $('.time');
   $('.time').remove();
   $(".the_notice").contents().wrapAll('<div class="wrap" />');
   $(".the_notice").prepend(temp);
于 2013-04-02T11:48:14.610 回答
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);
于 2013-04-02T11:58:40.470 回答
0

一行(或为清楚起见为 3 行):

$('.the_notice')
     .wrapInner($('<div class="wrap">'))
     .prepend($('.the_notice .time').detach());

JSFIDDLE

于 2013-04-02T12:03:08.457 回答