我有了这个在其外部JSON文件:遍历JSON数组中的jQuery
{
"Home": [
{ "link":"index.php" , "text":"Home" },
{ "link":"index.php?page=aboutus" , "text":"About Us" }
]
"Games": [
{ "link":"games.php" , "text":"All Games" },
{ "link":"games.php?name=game" , "text":"Game" }
]
}
这是我的JSON文件的基本设置。我计划在我的网站扩展时添加更多内容。我正在使用它作为我的菜单栏设置的一部分。它有3个按钮,主页,游戏和论坛。当您将鼠标移到主页按钮上时,我想将“主页”中的内容转变为链接并在我的网站上显示,与“游戏”按钮相同,当您将鼠标悬停在论坛按钮上时,不会显示任何内容。我试过这个:
编辑:再次稍微改变了功能,试图阻止它在每个鼠标上调用服务器,以及遍历它们并将它们显示在.navDD div标记中。
$(document).ready(function(){
var menu
$.getJSON('menu.json', function(data) {
menu = data;
});
$(".navlink").mouseenter(function(){
var find=$(this).text();
$.each(data,function(index){
$(".navDD").html(menu[find][index]['text']);
});
});
});
我确定我还是搞砸了。现在我得到了Unsigned TypeError:无法读取未定义的属性“长度”。我不确定我做错了什么。尽管在此之前我从未使用过JSON。
有人能帮我吗?
感谢, Legobear154
你甚至进入'$ .getJSON()'的回调函数吗? – arb
你有'http:// example.com/menu.json'或任何你的网络服务器上? –
@MarcB - 他必须,如果他得到了错误,他描述了 –