gpt4 book ai didi

javascript - 显示此元素而无需每次都重复代码

转载 作者:行者123 更新时间:2023-11-29 19:03:57 26 4
gpt4 key购买 nike

我在点击一个非常棒的标记时显示/隐藏来自 Json 的数据。

最佳答案

您可以像这样循环遍历 json 对象中的属性

var json = {
"France": [{
"image": "img/ausralia.jpg",
"description": "number django 1"
}],
"Australia": [{
"image": "img/ausralia.jpg",
"description": "number django 2"
}]
}

$(document).ready(function() {
// iterate all properties in the json object
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
// add the marker items
$(".marker." + prop.toLowerCase()).on("click", function( country ) {
$("#show").html(
"Image : <img src=" + json[country][0].image + ">" +
"| Description : <h1>" + json[country][0].description) + "</h1>";
}.bind( this, prop) ); // prop needs to be bound so that the correct country is triggered
}
}
});

关于javascript - 显示此元素而无需每次都重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44431426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com