2013-07-10 56 views
0

我如何收拾下面用jQuery這個HTML標籤,如何整理html標記與jQuery

<p><em>bla bla bla<br /></em>someone</p> 

成,

<p><em>bla bla bla</em><br />someone</p> 

我想唧唧歪歪到容器內,然後某人進入另一個容器。我想出了下面至今代碼...

  // Refresh quotes. 
      var quotes = $(".quote-library"); 

      // Starts with 1. 
      var count = 1; 
      $(".button-refresh").click(function(){ 

       var current = $("p:eq("+ count +")",quotes).text(); 
       alert(current); 
       //console.log(count + 1); 

       // Break the string into an array with the line break. 
       var array = current.split("\n"); 
       //alert(array[0]); 
       //console.log(count + 1); 

       // Put the text into the containers. 
       $(".quote-italic").empty().html("<em>"+ array[0] +"</em>"); 
       $(".quote-normal").empty().html(array[1]); 

       // Increase the counter on each click. 
       count ++; 

       // Increase the counter on each click. 
       count ++; 

       // Reset count counter when it reaches the limit. 
       if(count == $("p",quotes).length) count = 0; 
       return false; 
      }); 

HTML,

<div class="quote-library" style="display:none;"> 
    <p><em>Man is a creation of desire, not a creation of need.</em><br /> Gaston Bachelard</p> 
    <p><em>We are like butterflies who flutter for a day and think it's forever.</em><br /> Carl Sagan</p> 
    <p><em>Imagination will often carry us to worlds that never were. But without it we go nowhere.</em><br /> Carl Sagan</p> 
    <p><span><em>One must always maintain one's connection to the past and yet ceaselessly pull away from it.</em><br /> Jean-Paul Sartre<br /></span></p> 
    <p><span><em>Man is condemned to be free; because once thrown into the world, he is responsible for everything he does.</em><br /> Jean-Paul Sartre<br /></span></p> 
    <p><span><em>Hell is other people.</em><br /> Jean-Paul Sartre<br /></span></p> 
    <p><em>bla bla bla<br /></em>someone</p> 
</div> 

<div class="holder-quote"> 
    <p class="quote-voltaire"> 
     <span class="quote-italic"><i>A bottle of wine contains more philosophy than all the books in the world.</i></span> 
     <span class="quote-normal">Louis Pasteur</span> 
    </p> 
    <a href="#" class="button-refresh hide-text">refresh</a> 
</div> 

回答

0

這不是用記事本的問題++;這是您的JavaScript語法的問題。你會發現在你的問題上的語法突出顯示也有點不合適。

由於您在腳本中將它用作字符串數據,因此HTML片段需要放在字符串中:在此處輸入代碼$(「body」)。append('');

我認爲你也需要在引號中加上div標籤。

append(「div id = \」my_id \「>」);