b.createDocumentFragment不是函数(jQuery)

Ale*_*Fox 4 javascript twitter jquery function

我正在玩一个功能和获取

b.createDocumentFragment is not a function (jQuery)
Run Code Online (Sandbox Code Playgroud)

我的功能是

function tweetCount(url) {  
    $.getJSON("http://urls.api.twitter.com/1/urls/count.json?url="+url+"&callback=?", function(data) {
      count = data.count
      $(this).append(count);
   })
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多不同的方法,但似乎无法找出为什么它不喜欢"追加"."count"是一个数字,类似于alert(count)的工作,但不能追加!

有什么帮助?!亚历克斯

Chr*_*nte 5

我认为这并不是this指你的想法.更改$(this)为所需DOM元素的显式引用.

或者,您可以this通过调用来定义:

tweetCount.call($("#element"), url)
Run Code Online (Sandbox Code Playgroud)

编辑

试试这个:

$("span.tweetcount").each(function(){
    url = $(this).attr('title');
    tweetCount.call(this, url);
});
Run Code Online (Sandbox Code Playgroud)

或者,为了节省空间:

$("span.tweetcount").each(function(){
    tweetCount.call(this, $(this).attr('title'));
});
Run Code Online (Sandbox Code Playgroud)

编辑2:

尝试替换tweetCount为:

function tweetCount(url) {  
    var that = this;
    $.getJSON("http://urls.api.twitter.com/1/urls/count.json?url="+url+"&callback=?", function(data) {
        count = data.count;
        $(that).append(count);
})
Run Code Online (Sandbox Code Playgroud)