javascript - 为什么我无法在JQuery中获取变量值

标签 javascript jquery

我开发了使用 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饲料

最佳答案

尝试

$(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>')
                }
            }
        });
    });
});

关于javascript - 为什么我无法在JQuery中获取变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173027/

相关文章:

javascript - cordova-dialogs-plugin 中的数字输入

javascript - 如何取出双数组中的对象

jquery - CSS 粘性菜单高度调整不正确

javascript - 使用滚动在 jquery 中提交表单

javascript - 创建一个在点击按钮 x 次后消失的加载屏幕

javascript - "Uncaught TypeError: function is not iterable"在 [] 中使用 ... 但在 {} 中有效...?

javascript - 重新加载页面后如何获取最后一个索引

javascript - "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."我尝试了建议的解决方案,但它仍然不起作用

javascript - 查询 : Sliding Puzzle Generator Giving Unsolvable Puzzles

javascript - 比较两个具有不同格式的 Javascript 日期