尝试使用 json 文件和 Getjson 来填充
这是我到目前为止所拥有的。我正在尝试解析 json 文件并获取 img 路径和宽度,获取 GETJson json 中每条记录的返回值,并将其附加到“Gallery”内一个名为thumbs 的类的无序列表中。因此,我可以读取 json 文件并填充它,而不是在 html 中添加几行代码“li img src= path of images” width= "。
我的 json 如下所示:
[ { "image":"滑块/img/img1.jpg", “宽度”:400 }, { "image":"滑块/img/img2.jpg", “宽度”:400 }, ]
这是Getjson逻辑。
<script type="text/javascript">
$().ready(function(){
$.getJSON('fred.json', function(data) {
//Collection of li elements
var items = [];
$.each(data, function(key, val) {
items.append('<li><img src="' + val.image + '+ '" width="'
+ val.width + '" /></li>');
});
$('<ul/>', {
'class': 'thumbs',
html: items.join('')
}).appendTo('#Gallery');
//Once all ul are created call the gallery function
$('#Gallery').flickrGallery();
});
});
</script>
感谢您的帮助
我在 json 文件描述中错误地输入了 img 名称。我已经拼写正确了。当我运行 html 时,会发生两件事。图像根本不显示。其他一切都出现背景等图像确实出现。当我执行警报(项目)时,我可以看到 li 正在填充。 基本上,我有一个名为thumbs_1的div id=Gallery和ul,其class =thumbs
div id = 画廊
ul id="thumbs_1" class="thumbs"
李> "img src="images/image_11.jpg" width=600" 这样一共有11行。我只想使用 GetJson 动态放置同一行代码,而不是将其编码在 HTML 中。然后一旦 DIV 被填充
使用这个
$().ready(函数(){ $('#Gallery').flickrGallery({
调用特定函数 它已经开发出来并致力于创建一个照片库。 抱歉造成任何困惑,感谢您已经提供的帮助。
This is what I have so far. I'm trying to parse json file and grab img path and width get the return for each record in GETJson json and append to "Gallery" Inside an unordered list with a class named thumbs. So instead of having several lines of code "li img src= path of images" width= " in the html, I could just read json file and populate this.
My json looks like this:
[
{
"image":"sliders/img/img1.jpg",
"width":400
},
{
"image":"sliders/img/img2.jpg",
"width":400
},
]
Here's the Getjson logic.
<script type="text/javascript">
$().ready(function(){
$.getJSON('fred.json', function(data) {
//Collection of li elements
var items = [];
$.each(data, function(key, val) {
items.append('<li><img src="' + val.image + '+ '" width="'
+ val.width + '" /></li>');
});
$('<ul/>', {
'class': 'thumbs',
html: items.join('')
}).appendTo('#Gallery');
//Once all ul are created call the gallery function
$('#Gallery').flickrGallery();
});
});
</script>
Thank You for any help
I incorrectly type the img name in the json file description. I have it spelled out correctly. When I run the html two things happen. The images do not show at all. Everything else appears background etc. for the images does appear. When I do an alert(items) I can see the li populating.
Basically, I have a div id=Gallery and and ul inside the div named thumbs_1 with class =thumbs
div id = GALLERY
ul id="thumbs_1" class="thumbs"
li> "img src="images/image_11.jpg" width=600" there are a total of 11 lines like this. Instead of having this harcoded in the HTML I just want to dynamically place the same line of code using GetJson. Then once the DIV is populated
use this
$().ready(function(){
$('#Gallery').flickrGallery({
which calls a specfic function
that has been developed already and works to creates a photo gallery.
Sorry for any confusion and Thank you for already helping out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的json作为最后一个“}”之后的额外逗号,我实际上不确定你可以在数组中“.append”。您可以尝试类似的操作
,然后像这样在#gallery 中附加“items”var,
但我不太确定您要做什么。我假设您使用“alert()”测试了代码的每一部分,看看它是否运行良好。
Your json as an extra coma after the last "}" and I'm not actually sure you can ".append" in an array. You could try something like that
And then append the 'items' var in #gallery like this
But i'm not really sure about what you are trying to do. I assume you tested every bit of the code with "alert()" to see if it runs well.
如果没有 api 密钥,我无法让 flickrGallery 工作,但我创建了两个示例供您使用 GalleryView 进行查看。一种使用 jQuery 模板,一种不使用。
使用 JSON 的图库演示
在每个循环中尝试一下 (或看一下)
I couldn't get flickrGallery to work without an api key but I've created two examples for you to look through using GalleryView. One using jQuery templates and one without.
Gallery Demo with JSON
Try this in your each loop (or take a look)
你的 .each 中有一个额外的串联,应该使用 items.push 而不是 items.append (并且 JSON 末尾有一个额外的逗号)...以下作品(与你断言画廊创建作品)
you have an extra concatenation in your .each, and should use items.push instead of items.append (and there's that extra comma at the end of the JSON)...the following works (with your assertion that the gallery creation works)
/* ]]> */
/* ]]> */