如何保存文件“ main.js”,以便不从文件“ file_name.json”中加载数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何保存文件“ main.js”,以便不从文件“ 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/
代替具有完成功能和日期变量的getJSON,必须插入分配给数据变量的JSON。您可能需要使用done函数删除getJSON并将JSON手动分配给数据变量。
如何保存文件main.js,以便它不会从文件file_name.json加载数据?
我将非常感谢您提供有效的帮助。
答案
所以我注释了getJSON函数并在file_name.json
变量中使用了mydata
文件的复制内容
$
(
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
//)
}
) ;
以上是关于如何保存文件“ main.js”,以便不从文件“ file_name.json”中加载数据?的主要内容,如果未能解决你的问题,请参考以下文章
如何将非通用排序列表保存到文件中,以便可以从文件中加载该排序列表?
如何将变量保存到新的文本文件中,以便下次程序运行时加载这些变量?