2013-04-02 3 views
5

Я что-то вроде:Обертывание некоторую часть HTML с помощью JQuery

<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? Пожалуйста, помогите мне в этом.

ответ

5

Ну, если содержимое всегда будет то же самое, что вы можете сделать что-то вроде

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

http://jsfiddle.net/Ysq48/

+0

спасибо, сэр, вы просто меня спасете. –

0

Demo

var temp = $('.time'); 
    $('.time').remove(); 
    $(".the_notice").contents().wrapAll('<div class="wrap" />'); 
    $(".the_notice").prepend(temp); 
+0

Это не работает, вы удаляете ' .time' и обертывание каждого отдельного ребенка. – iappwebdev

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); 
0

С одной линии (или 3 для ясности):

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

JSFIDDLE

Смежные вопросы