【问题标题】:Why I cant get the variable value in JQuery [duplicate]为什么我无法在 JQuery 中获取变量值 [重复]
【发布时间】:2014-04-06 01:14:06
【问题描述】:

我开发了使用 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 提要

【问题讨论】:

  • 这是跨域请求吗?
  • 首先,在 ajax 请求中你不能这样做 - stackoverflow.com/questions/14220321/…
  • 第二个因为cont是一个变量th.append("&lt;h1&gt;"+cont+"&lt;/h1&gt;")
  • 这个问题不值得投 2 票。
  • @Liam 下面的答案效果很好。非常感谢您的宝贵时间。

标签: javascript jquery


【解决方案1】:

试试

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

【讨论】:

  • 非常感谢,效果很好。当我能够做到这一点时(6 分钟后),我会选择你的答案作为正确答案。
猜你喜欢
  • 1970-01-01
  • 2014-12-01
  • 2022-08-24
  • 2021-11-06
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多