我有一个脚本,当前通过json文件和ajax a获取引导程序轮播的数据。它工作正常,已经工作了一段时间。我遇到的问题是,我现在正在尝试制作一个新的轮播显示,以四个一组而不是单个幻灯片的形式显示各个幻灯片作为缩略图。我不确定在每个循环中将数组拆分为4个组的正确逻辑。任何建议,将不胜感激。下面是我当前的JQUERY脚本,用于获取幻灯片的数据,以及新布局的HTML。
JQUERY
$(document).ready(function(){
$.getJSON("/property/slideshow_json", function(data){
}).success(function(data){
$.each(data, function (index, value) {
$(".carousel-indicators").append($( '<li data-target="#myCarousel" data-slide-to="' +index+ '">'+ (index+1) +'</li>' ));
$(".carousel-inner").append($('<div class="item"><img src="/property/photo/'+value.mls_number+'/1"><div class="carousel-caption"><h4>PRICED TO SELL | $'+value.price+'</h4><p style="text-transform:uppercase;">'+value.address+' <a href="/property/detail/'+value.mls_number+'"class="btn btn-small btn-info pull-right">View Details</a></p></div></div>'));
});
$('.carousel-indicators li:first').addClass('active');
$('.carousel-inner div:first').addClass('active');
$('#myCarousel').carousel({interval: 4000});
});
});
上述脚本需要输出的示例。
<div class="span12">
<h2>Featured Listings</h2>
<div id="featured" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#featured" data-slide-to="0" class="active"></li>
<li data-target="#featured" data-slide-to="1"></li>
<li data-target="#featured" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<div class="row-fluid">
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
</div>
</div>
<div class="item">
<div class="row-fluid">
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
</div>
</div>
<div class="item">
<div class="row-fluid">
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
<div class="span3"><a href="#x" ><img src="http://placehold.it/250x250" alt="Image" style="max-width:100%;" /></a></div>
</div>
</div>
</div>
<a class="carousel-control left" href="#featured" data-slide="prev">‹</a>
<a class="carousel-control right" href="#featured" data-slide="next">›</a>
</div>
</div>
答案 0 :(得分:1)
要么以不同的方式从服务器获取数据,必须使用jQuery对其进行操作。就个人而言,我会在服务器端执行此操作,并以正确的格式接收json。
然而,有很多方法可以用jQuery做到这一点,这是一个:
var counter = 0,
indicators = 0,
items = '<div class="item active"><div class="row-fluid">';
$.each(data, function (index, value) {
switch(counter) {
case 4: $(".carousel-indicators").append($( '<li data-target="#featured" data-slide-to="' +indicators+ '"></li>' ));
$(".carousel-inner").append(items + '</div></div>');
// increment indicators
indicators++;
// "reset" items
items = '<div class="item active"><div class="row-fluid">';
break;
default:
// you might need something more here.
// I didn't saw in your html sample value.address and value.mls_number
items += '<div class="span3"><a href="#x" ><img src="/property/photo/'+value.mls_number+'/1" alt="Image" style="max-width:100%;" /></a></div>';
}
counter++;
});