单击元素时调用此函数。它根据单击的元素创建一个字符串,然后在 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"
}