JSON与Javascript的交互
我需要以10个为一组显示JSON数据的结果。这会将整个列表拉入集合:JSON与Javascript的交互,javascript,jquery,json,Javascript,Jquery,Json,我需要以10个为一组显示JSON数据的结果。这会将整个列表拉入集合: function populateSavedData(select, data) { var items = []; $.each(data, function (id, option) { items.push('<li><div class="slider-product"><img class="slider-product-img" src="' + option.prod
function populateSavedData(select, data) {
var items = [];
$.each(data, function (id, option) {
items.push('<li><div class="slider-product"><img class="slider-product-img" src="' +
option.product_image_url + '"/><div class="buy-it"><a href="' + option.exit_url + '"><img
src="http://i.cdn.turner.com/v5cache/TBS/images/shows/cougartown/buy-it-button.png?" border="0">
</a></div><div class="product-info">'+ option.product_name + '</div></div></li>');
});
select.html(items.join(''));
}
populateSavedData($('ul'), json);
我该如何改变它,在一个div中显示一组前10个,在另一个div中显示一组后10个?我对js和json都是新手
这是我的JSFIDLE:而不是
select.html(items.join(''));
做
您必须实现getDiv函数,该函数返回要插入元素的嵌套div或元素
Array.splice takes返回一个切片或数组,修改原始数组。基本上,在每次迭代中,我从数组的开始处提取10个项,直到没有剩余项为止。我将按照以下思路进行处理:警告,未测试,但原则是正确的:
var cnt = 0;
$.each(data, function (id, option) {
if(cnt++ % 10 === 0) {
if(items.length) items.push('</div>');
items.push('<div>');
}
items.push('...');
});
select.html(items.join(''));
顺便说一句,你的LI标签很忙,结构噪音很大。您应该能够将其简化为以下内容:
<li><a>...</a></li>
并且去掉那些讨厌的div,即你当前的代码有点受影响。你能编辑你的帖子吗?在注释中很难理解您的代码。我添加了一个JS Fiddle链接。
<li><a>...</a></li>