0

单击元素时调用此函数。它根据单击的元素创建一个字符串,然后在 JSON 对象中查找与该字符串关联的值。然后它会提醒该值。这是它的外观:

function alertHelp(){
    var option = $(this).context.parentNode.textContent.substring(0, $(this).context.parentNode.textContent.length - 2);
    $.getJSON("Messages.json", function(json, option) {
        alert(json[option]);
    });
}

我发现该选项设置正确,但是当传递给带有警报的函数时发生了变化。如果我alert(option)之前alert(json[object]);,我会收到一个简单地说“成功”的警报。不知道这是怎么回事。alert(json[option])只是提醒“未定义”。

这是 Messages.json:

{
    "Space Control": "Red = a square black controls. Green = a square white controls. Yellow = a square contested by both players",
    "Legal Moves": "Click on a piece and a blue dot will appear on all the squares that piece can move to",
    "PieceFlair": "When you move a piece, all the pieces that come under attack as a direct result of the piece you moved will pulse white",
    "Forks": "All the moves that would result in a simultaneous attack of two pieces are shown",
    "Pins": "Not yet implemented"
}
4

1 回答 1

2

你已经被参数“掩盖”var option了,试试这个:functionoption

var text = $(this).context.parentNode.textContent;
var option = text.substring(0, text.length - 2);

$.getJSON("Messages.json", function(json, status) {
    alert(status);  // success
    alert(json[option]);
});
于 2013-08-10T19:30:19.303 回答