1

我是学习代码的新手 - 所以请耐心等待。我正在尝试为我的妻子(她是一名学校老师)创建一个危险风格的游戏。但是,我遇到了一个问题。我想单击一个链接(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>);

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

是否有任何想法或我以错误的方式接近这个项目?

4

2 回答 2

0

我不熟悉危险,但列表应该包含从 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] );
    });
});
于 2013-04-30T06:25:59.243 回答
0

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

   var arr = [[
       {
          property: value 
       }, {
          property: value 
       }][{
          property: value 
       }, {
          property: value 
       }]
    ];

我为元素添加了一个class调用,然后您可以使用 jQuery 的内置索引(在我们的例子中)对其进行迭代,然后迭代ul 中的元素数量以从数组中添加一个问题。rowsuleach()parows

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

jsFiddle

于 2013-04-30T06:27:40.500 回答