我开发了使用JQuery获取JSON数据的工具,但问题是一些变量没有放置它们的值而且我的变量名称只有文本。这个代码
$(document).ready(function(){
$('.HTML').each(function(){
var call = $.getJSON('http://test-khamsat-support-gig.blogspot.com/feeds/posts/default/-/Break?max-results=10&orderby=published&alt=json-in-script&callback=?',function(data){
var cont = data.version;
});
var th = $(this);
var b = th.html(),
a = b.match(/[^[\]]+(?=])/g);
if(a){
if(a[2] == 'one'){
th.append("<h1>'+cont+'</h1>")
}
}
});
});
代码将在我的博客中运行,因为它内容为blog Feed
答案 0 :(得分:4)
尝试
$(document).ready(function () {
$('.HTML').each(function () {
//store the reference to the element to use inside the callback
var th = $(this);
$.getJSON('http://test-khamsat-support-gig.blogspot.com/feeds/posts/default/-/Break?max-results=10&orderby=published&alt=json-in-script&callback=?', function (data) {
var cont = data.version;
//move this inside the ajax callback to support async nature
var b = th.html(),
a = b.match(/[^[\]]+(?=])/g);
if (a) {
if (a[2] == 'one') {
//use proper string concatenation here
th.append('<h1>' + cont + '</h1>')
}
}
});
});
});