Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析JSON并将值记录在不同的div中_Javascript_Jquery_Json - Fatal编程技术网

Javascript 解析JSON并将值记录在不同的div中

Javascript 解析JSON并将值记录在不同的div中,javascript,jquery,json,Javascript,Jquery,Json,我是学习代码的新手,所以请耐心等待。我正在尝试为我的妻子创建一个危险风格的游戏,她是一名学校老师。然而,我遇到了一个问题。我想点击一个链接$100等,并将线索值记录到一个单独页面上的div中。通过研究,我决定从数组创建一个JSON对象,然后对JSON进行索引和值解析 这是我的HTML: <ul id="rowOne"> <li><a href="clueJ.html">$100</a></li> <li>&l

我是学习代码的新手,所以请耐心等待。我正在尝试为我的妻子创建一个危险风格的游戏,她是一名学校老师。然而,我遇到了一个问题。我想点击一个链接$100等,并将线索值记录到一个单独页面上的div中。通过研究,我决定从数组创建一个JSON对象,然后对JSON进行索引和值解析

这是我的HTML:

<ul id="rowOne">
    <li><a href="clueJ.html">$100</a></li>
    <li><a href="clueJ.html">$100</a></li>
    <li><a href="clueJ.html">$100</a></li>
    <li><a href="clueJ.html">$100</a></li>
    <li><a href="clueJ.html">$100</a></li>
</ul>
以下是我希望记录值的div:

<div id="clueContainer" class="center"></div>
以下是我的Jquery:

$(document).ready(function(){

var rowOne = array [

{name:"$100", value:"On the world political map, where were some of the major states and empires located about 1500 A.D. (C.E.)?"},
{name:"$200", value:"What were the artistic, literary, and intellectual ideas of the Renaissance?"},
{name:"$300", value:"Where were the five world religions located around 1500 A.D. (C.E.)?"},
{name:"$400", value:"What were the regional trading patterns about 1500 A.D. (C.E.)?"},
{name:"$500", value:"Why were the regional trading patterns important?",}

];

var json = JSON.parse(rowOne);

$("li a href").click(function(){
$.each(json, function(index, value){
    $("#clueContainer").append(function(){
    $(value).log(<p>+value+</p>);

});
});
});
});

是否有任何想法或我以错误的方式处理此项目?

我不熟悉jeopardy,但列表中应该包含从100美元到500美元的值? 我修改了您的代码,假设列表是升序的:

没有代码就没有小提琴:

$(document).ready(function () {

    var rowOne = {
        "$100": "On the world political map, where were some of the major states and empires located about 1500 A.D. (C.E.)?",
        "$200": "What were the artistic, literary, and intellectual ideas of the Renaissance?",
        "$300": "Where were the five world religions located around 1500 A.D. (C.E.)?",
        "$400": "What were the regional trading patterns about 1500 A.D. (C.E.)?",
        "$500": "Why were the regional trading patterns important?"
    };

    $("li").on('click', 'a', function () {
        var foo = $(this).text();
        $('#clueContainer').text( rowOne[foo] );
    });
});

对于数组,您的想法是正确的,但是语法与您的略有不同。我已经创建了一个对象数组。结构与此类似:

   var arr = [[
       {
          property: value 
       }, {
          property: value 
       }][{
          property: value 
       }, {
          property: value 
       }]
    ];
我在ul元素中添加了一个名为rows的类,然后在我们的例子中,您可以使用jQuery的内置索引p对每个类进行迭代,然后迭代ul中的元素数,从rows数组中添加一个问题


href不是HTML标记。去掉那个。接下来,单击链接时加载clueJ.html可能会遇到麻烦。最好的办法是你把它们换成新的。我也不明白$value.log是从哪里来的。。。你在使用插件吗?最后,+value

要求标记用引号括起来:“”+value+“

”。我没有使用插件。我可以只使用$clueContainer.append''+value+'

'吗?确实可以。您甚至可以省略标记。jQuery将自动为您生成一个文本节点。或者,您可以使用$'clueContainer'.textvalue。啊,太晚了。有人把它作为他的答案的一部分。Jeopardy有多行和多列。该值在列中按升序增加,而不是在行中。但还是;拥有数组对象不是更好吗?因为每一行都有相同的名称,所以我们可以使用该名称作为对象的属性,并分配一个以问题作为项的数组。这会使问题的设置变得简单一些。不……我来自Javascript/jquery背景。实际上,我的阵列看起来和你的一样。@Derija93,我想我在不该做的时候做了些改变。现在他们有了不同的名字。@justLearning,噢,好的。只是想知道。你的数组看起来与PHP数组语法非常相似。仍然会出现一个空白屏幕,我的数组没有问题。
$(document).ready(function () {
    var rows = [
        [{
            name: "$100",
            value: "On the world political map, where were some of the major states and empires located about 1500 A.D. (C.E.)?"
        }, {
            name: "$200",
            value: "What were the artistic, literary, and intellectual ideas of the Renaissance?"
        }, {
            name: "$300",
            value: "Where were the five world religions located around 1500 A.D. (C.E.)?"
        }, {
            name: "$400",
            value: "What were the regional trading patterns about 1500 A.D. (C.E.)?"
        }, {
            name: "$500",
            value: "Why were the regional trading patterns important?"
        }],
        [{
            name: "$100",
            value: "Who cares about the price of tea in China?"
        }, {
            name: "$200",
            value: "Why did the chicken cross the road?"
        }, {
            name: "$300",
            value: "How much wood could a woodchuck chuck if a woodchuck could chuck wood?"
        }, {
            name: "$400",
            value: "How many SEO specialists does it take to change a light, bulb, lightbulb, energy, electricity?"
        }, {
            name: "$500",
            value: "Where in the world is Carmen San Diego?"
        }]
    ];
    $(".rows").each(function (p) {
        $("#row" + p).find("a").each(function (i) {
            $(this).text(rows[p][i].name);
            $(this).click(function () {
                var value = rows[p][i].value;;
                $("#clueContainer").html(value);
            });
        });
    });
});