在动态加载的元素上使用jQuery

时间:2012-11-01 21:05:03

标签: javascript jquery facebook dynamic

我正在使用以下代码从Facebook Graph API中检索喜欢的数量,这非常有效。我遇到的麻烦是使用jQuery .css来改变返回值中最后一个字符的颜色。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {
        //Set Url of JSON data from the facebook graph api. make sure callback is set   with a '?' to overcome the cross domain problems with JSON
        var url = "https://graph.facebook.com/krewella?callback=?";

        //Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
        $.getJSON(url,function(json){
            var html = "<ul><li>" + json.likes + "</li><li>" + json.about + "</li></ul>";
            //A little animation once fetched
            $('.facebookfeed').animate({opacity:0}, 500, function(){
                $('.facebookfeed').html(html);
            });
            $('.facebookfeed').animate({opacity:1}, 500);
        });
      });
    </script>

</head>

<body>
    <div id="wrapper"><!--wrapper open-->
        <div class="facebookfeed">
        <h2>Loading...</h2>
    </div>
    </div><!--wrapper closed-->
</body>

1 个答案:

答案 0 :(得分:0)

如下:

json.likes = json.likes.toString().slice(0,-2) + "<span class='red'>" + json.likes.toString().substr(-1) + "</span>"
var html = "<ul><li>" + json.likes + "</li><li>" + json.about + "</li></ul>";

琐碎的CSS:

.red{
    color:#ff0000;
}