2013-04-22 22 views
0

你好我試圖自動化內容從文本共享此鏈接此內容...自動創建份額從第

有每一頁上的20個職位,將需要鏈接,即爲什麼我做了# tweet01

這是我的存根,但我認爲我錯過了一些東西,任何幫助非常感謝,在此先感謝!

<p id="tweet01">When I was 10 I started washing cars. I then offered to clear paths for those without cars. I soon made money from most houses on my street.</p> 

    <div class="stream-item-footer"> 
    <ul class="tweet-actions"> 
     <script type="text/javascript"> 
      var tweet = $("#tweet01").text() 
      var shortTweet = $("#tweet01").text() // get the text within the div 
      .trim() // remove leading and trailing spaces 
      .substring(0, 100) // get first 100 characters 
      .split(" ") // separate characters into an array of words 
      .slice(0, -1) // remove the last full or partial word 
      .join(" ") + "..."; // combine into a single string and append "..." 
      $(document).ready(function() { 
        $().html('<li><span class="st_twitter" st_url="http://www.domain.com" st_title="'+ ++shortTweet +'"></span></li>'); 
        $().html('<li><span class="st_linkedin" st_url="http://www.domain.com" st_title="'+ ++tweet +'" st_summary="'+ ++tweet +'"></span></li>'); 
        $().html('<li><span class="st_facebook" st_url="http://www.domain.com" st_title="'+ ++tweet +'" st_summary="'+ ++tweet +'"></span></li>'); 
        $().html('<li><span class="st_googleplus" st_url="http://www.domain.com" st_title="'+ ++tweet +'" st_summary="'+ ++tweet +'"></span></li>'); 
      }); 
     </script> 
    </ul> 
    </div> 
+1

有什麼問題嗎? – tymeJV 2013-04-22 15:02:18

+0

'$()。html('');'什麼都不做。 '$()'什麼也不選,所以這就是返回的結果。 – 2013-04-22 15:08:34

+0

只是一些建議,而不是在每條推文後放這段代碼,只寫一次這段代碼。給每個推文同一個類,然後執行'$('。tweet')。each(function(){// ...});'。 – 2013-04-22 15:10:50

回答

0

你想要做的是將這段代碼移動到頁面頂部,並在頁面準備就緒時運行。然後,你想要做的是,循環每個推文(在給予他們所有相同的類別之後),並追加到其各自的.tweet-actions

像這樣:

<p id="tweet01" class="tweet">When I was 10 I started washing cars. I then offered to clear paths for those without cars. I soon made money from most houses on my street.</p> 
<div class="stream-item-footer"> 
    <ul class="tweet-actions"></ul> 
</div> 

<p id="tweet02" class="tweet">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce eleifend pharetra risus non facilisis. Sed sagittis, felis vel viverra metus.</p> 
<div class="stream-item-footer"> 
    <ul class="tweet-actions"></ul> 
</div> 

<p id="tweet03" class="tweet">Bacon ipsum dolor sit amet flank shoulder pork chop, turducken shankle strip steak capicola biltong jerky. Salami filet mignon short ribs pork chop.</p> 
<div class="stream-item-footer"> 
    <ul class="tweet-actions"></ul> 
</div> 

然後,在你的頁面的頂部添加此腳本:

<script> 
    $(function() { 
     $('.tweet').each(function() { 
      var tweet = $(this).text(); 
      var shortTweet = tweet // get the text within the div 
      .trim() // remove leading and trailing spaces 
      .substring(0, 100) // get first 100 characters 
      .split(" ") // separate characters into an array of words 
      .slice(0, -1) // remove the last full or partial word 
      .join(" ") + "..."; // combine into a single string and append "..." 
      var $actions = $(this).next('.stream-item-footer').find('.tweet-actions'); 

      $actions.append('<li><span class="st_twitter" st_url="http://www.domain.com" st_title="' + shortTweet + '">Twitter</span></li>'); 
      $actions.append('<li><span class="st_linkedin" st_url="http://www.domain.com" st_title="' + tweet + '" st_summary="' + tweet + '">LinkedIn</span></li>'); 
      $actions.append('<li><span class="st_facebook" st_url="http://www.domain.com" st_title="' + tweet + '" st_summary="' + tweet + '">Facebook</span></li>'); 
      $actions.append('<li><span class="st_googleplus" st_url="http://www.domain.com" st_title="' + tweet + '" st_summary="' + tweet + '">Google+</span></li>'); 
     }); 
    }); 
</script> 

DEMO:http://jsfiddle.net/XHgtg/

+0

完美,正是我所需要的,非常感謝火箭隊! – OnsiteDesigner 2013-04-22 15:47:44

+0

不客氣! :-D – 2013-04-22 15:48:07