0

嗨,我正在尝试自动从文本链接内容以分享...</p>

每个页面上有 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>
4

1 回答 1

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>

演示:http: //jsfiddle.net/XHgtg/

于 2013-04-22T15:31:23.503 回答