JSON + jQuery 不工作

发布于 2024-11-03 03:37:00 字数 1423 浏览 0 评论 0原文

我试图让 jQuery 获取 JSON 文件,并在按下按钮时将其中的数据放在一个简单的站点上。 因此,JSON 代码如下所示:

{
    "images" : [
        { "source" = "images1", "alternative" = "altImg1" },
        { "source" = "images2", "alternative" = "altImg2" },
        { "source" = "images3", "alternative" = "altImg3" }
    ]
}

HTML + jQuery:

<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
        <title>jQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  
    </head>

    <body>
        <button>Press Me!</button>
        <script>
            $('button').click(function()    {
                $.getJSON('json-db.html', function(data)    {
                    for(var i = 0; i < data.images.length; i++) {
                        var image = data.images[i];
                        $('#result').append('<h1>' + image.source + ' ' + image.alternative + '</h1>');
                    }
                });
            });
        </script>
        <div id="result">Result</div>
    </body>
</html>

Firebug 未检测到错误。我多次重写代码,查找错误,将其与类似代码进行比较等等,但找不到任何东西。

提前致谢!

I'm trying to make jQuery take JSON file and put the data from it on a simple site, when a button is pressed.
So, the JSON code looks like this:

{
    "images" : [
        { "source" = "images1", "alternative" = "altImg1" },
        { "source" = "images2", "alternative" = "altImg2" },
        { "source" = "images3", "alternative" = "altImg3" }
    ]
}

And the HTML + jQuery:

<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
        <title>jQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  
    </head>

    <body>
        <button>Press Me!</button>
        <script>
            $('button').click(function()    {
                $.getJSON('json-db.html', function(data)    {
                    for(var i = 0; i < data.images.length; i++) {
                        var image = data.images[i];
                        $('#result').append('<h1>' + image.source + ' ' + image.alternative + '</h1>');
                    }
                });
            });
        </script>
        <div id="result">Result</div>
    </body>
</html>

There are no errors detected by Firebug. I rewrote the code several times, looked for mistakes, compared it to a similar code and so on, but couldn't find anything.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-11-10 03:37:00

您的 json 表示法是错误的,

请使用 : 而不是 = ,例如:

..........
"images" : [
    { "source" : "images1", "alternative" : "altImg1" },
    ....................
]
..........

your json notation is wrong

use : instead of = like:

..........
"images" : [
    { "source" : "images1", "alternative" : "altImg1" },
    ....................
]
..........
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文