Lay*_*yne 1 javascript arrays variables global-variables multidimensional-array
我在页面上有50个点,每个单独的div.当我单击一个时,我想使用ID从数组中提取值.我可以获取ID,但是我无法使用该值从我的数组中获取内容.也许全局变量问题?不确定.甚至不确定这是否是处理访问多个数据的多次点击的最佳方式.任何帮助表示赞赏!
var location0 = {"name" : "Location 1", "image" : "image1.jpg"};
$('.dot').click(function(){
var thisLocation = $(this).attr("id");
alert(thisLocation); //Returns "location0"
alert(thisLocation["image"]); //Returns "undefined"
});
Run Code Online (Sandbox Code Playgroud)
我这样做:
var locations = {
location1 : {"name" : "Location 1", "image" : "image1.jpg"},
location2 : {"name" : "Location 2", "image" : "image2.jpg"}
}
$('.dot').click(function(){
alert(locations[this.id].name);
});
?
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
915 次 |
最近记录: |