我试图访问手风琴菜单中的元素。我使用jQuery加载JSON数据填充手风琴菜单。我可以看到当页面加载时正在创建完整菜单。问题是我无法访问菜单中的第二级。使用jQuery访问HTML内部的元素
JSON文件( “cwdata.json”):
{
"Country": [{
"CountryName": "COUNTRY NAME GOES HERE",
"Region": [{
"RegionName": "REGION NAME GOES HERE",
"SubRegion": [{
"SubRegionName": "SUBREGION NAME GOES HERE"
}]
}]
}]
}
的jQuery用来填充从JSON文件手风琴菜单:
$.getJSON('../JSON/cwdata.json', function (cwData) {
$.each(cwData.Country, function (i, country) { // loop through all the countries
var countries = '<li class="country_name"><a href="#">' + country.CountryName + '</a></li>'; // the name of the country
var country_region = '<ul class="country_region">'; // create a list of all the regions in the country
var region_subregion = "";
$.each(country.Region, function (i, region) { // loop through all the regions
country_region += '<li class="region_name"><a href="#">' + region.RegionName + '</a></li>'; // the name of the region
region_subregion = '<ul class="region_subregion">'; // create a list of all the subregions in the region
$.each(region.SubRegion, function (i, subregion) { // loop through all the regions
region_subregion += '<li class="subregion_name"><a href="#">' + subregion.SubRegionName + '</a></li>'; // the name of the subregion
});
region_subregion += '</u>'; //close the list tags
});
country_region += '</ul>'; //close the list tags
$(countries).appendTo('#Country').append(country_region); // append the region to the country and append the country to the accorion menu
$(region_subregion).appendTo("li.region_name"); // append the subregion to the region
});
}); // getJSON
在菜单的第一项(“首页“),当我点击它时会起作用。但是,当我点击它时,“家”下的任何项目都没有任何反应或反馈。
这是页面加载后的HTML菜单:
我可以使用此代码来检查元素是什么,当我点击它:
$("a").click(function (event) {
alert("Value: " + event.target);
});
如果我点击“首页”,它返回:"Value: HTMLSpanElement"
。但是当我点击“国家名称向此处”时,没有任何反应。我不能让这对小提琴要么工作,但这里是我能提供什么:
http://fiddle.jshell.net/3d6Nz/2/
中的jsfiddle你必须选择jQuery库首先在左侧面板上。 – Banana
“我无法在小提琴上工作” –