[英]JavaScript global not keeping scope? creating an autocomplete webservice json object
[英]JavaScript scope with JSON object
单击元素时将调用此函数。 它根据单击的元素创建一个字符串,然后在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(json[object]);
之前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"
}
你已经通过function
参数option
“遮蔽”你的var option
option
,试试这个:
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]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.