[英]How to save the file “main.js” so that it doesn't load data from the file “file_name.json”?
從main.js文件中加載帶有圖像路徑的數據。 目前,此數據是從file_name.json文件加載的。
保存file_name.json文件:
{ "images": [ {"file_name":"https://source.unsplash.com/1600x900/?fitness","alt":"","href":"#"}, {"file_name":"https://source.unsplash.com/1600x900/?yoga","alt":"","href":"#"}, {"file_name":"https://source.unsplash.com/1600x900/?workout","alt":"","href":"#"}, {"file_name":"https://source.unsplash.com/1600x900/?running","alt":"","href":"#"}, {"file_name":"https://source.unsplash.com/1600x900/?girl","alt":"","href":"#"}, {"file_name":"https://source.unsplash.com/1600x900/?cat","alt":"","href":"#"} ], "others": [ ] }
這是main.js文件中的一小段代碼:
( function() { $.getJSON("data/file_name.json").done ( function(data) { <-----remaining code-----> } ) } );
這是來自 JSFiddle 的所有代碼: https://jsfiddle.net/Krzysiek_35/d8yz6g5r/32/
您必須插入分配給數據變量的 JSON,而不是使用完成的 function 和日期變量的 getJSON。 您可能需要使用完成的 function 刪除 getJSON 並手動將 JSON 分配給數據變量。
如何保存文件main.js以便它不會從文件file_name.json加載數據?
我將非常感謝有效的幫助。
所以我評論了 getJSON function 並在mydata
變量中使用file_name.json
文件的復制內容
$
(
function()
{
//$.getJSON("data/file_name.json").done
//(
var mydata = `{
"images":
[
{"file_name":"https://source.unsplash.com/1600x900/?fitness","alt":"","href":"#"},
{"file_name":"https://source.unsplash.com/1600x900/?yoga","alt":"","href":"#"},
{"file_name":"https://source.unsplash.com/1600x900/?workout","alt":"","href":"#"},
{"file_name":"https://source.unsplash.com/1600x900/?running","alt":"","href":"#"},
{"file_name":"https://source.unsplash.com/1600x900/?girl","alt":"","href":"#"},
{"file_name":"https://source.unsplash.com/1600x900/?cat","alt":"","href":"#"}
],
"others":
[
]
}`;
var dataobj = JSON.parse(mydata); //convert JSON -> Javascript Object
(function(data)
{
//..hidden unrelated code
})(dataobj);
//This function style is known as IIFE - https://developer.mozilla.org/en-US/docs/Glossary/IIFE
//)
}
) ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.