2013-04-02 11 views
5

mam coś takiego:Owijanie jakąś część HTML za pomocą 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> 

W rezultacie chcę coś takiego:

<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> 

Jak mogę osiągnąć to za pomocą jQuery? Pomóż mi w tym.

Odpowiedz

5

Dobrze, jeśli zawartość będzie zawsze taka sama, można zrobić coś takiego

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

http://jsfiddle.net/Ysq48/

+0

dzięki Sir, po prostu mnie ocal. –

0

Demo

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

to nie zadziała, usuwasz ' .time' i owijanie każdego dziecka. – iappwebdev

0

przeczytać ten link może pomóc: What is the Most effcient way to create html elements using jquery

Moja odpowiedź jest odpowiedzią dobrze być tak:

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

Z jednej linii (lub 3 dla jasności):

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

JSFIDDLE

Powiązane problemy