其他问题的发布与此类似,但我的问题只是我不知道在哪里提供信息。也就是说,我试图限制从下面脚本中的列表中提取的项目数。脚本本身工作正常,我只需要将项目数量限制为六而不是一切。我觉得这很简单,但我从其他帖子插入的每一行代码都不适合我。有什么帮助吗?
(function($){
$.ajax({
type: "GET",
url: "/homepage_photo_slider/PhotoGallery.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('img').each(function() {
var location = '/homepage_photo_slider/';
var url = $(this).attr('src');
var alt = $(this).attr('alt');
$('<li></li>').html('<a href="'+location+''+url+'" class="pirobox" rel="gallery" title="'+alt+'"><img class="thumb" src="'+location+''+url+'" alt="'+alt+'" title="'+alt+'" /></a>').appendTo('#gallery-ul');
});
$().piroBox_ext({
piro_speed : 700,
bg_alpha : 0.5,
piro_scroll : true
});
}
});
})(jQuery);
答案 0 :(得分:0)
未经测试,但我认为这应该有效。我只更改了一行,在评论中标出。在应用each
之前,我使用切片方法将数组修剪为六个元素:
(function($){
$.ajax({
type: "GET",
url: "/homepage_photo_slider/PhotoGallery.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('img').slice(0,6).each(function() { // <--- CHANGED LINE HERE
var location = '/homepage_photo_slider/';
var url = $(this).attr('src');
var alt = $(this).attr('alt');
$('<li></li>').html('<a href="'+location+''+url+'" class="pirobox" rel="gallery" title="'+alt+'"><img class="thumb" src="'+location+''+url+'" alt="'+alt+'" title="'+alt+'" /></a>').appendTo('#gallery-ul');
});
$().piroBox_ext({
piro_speed : 700,
bg_alpha : 0.5,
piro_scroll : true
});
}
});
})(jQuery);
答案 1 :(得分:0)
尝试使用for循环而不是.each()方法:
var $images = $(xml).find('img');
for(var i = 0; i < 6; i++)
{
var $image = $($images[i])
var location = '/homepage_photo_slider/';
var url = $image.attr('src');
var alt = $image.attr('alt');
$('<li></li>').html('<a href="'+location+''+url+'" class="pirobox" rel="gallery" title="'+alt+'"><img class="thumb" src="'+location+''+url+'" alt="'+alt+'" title="'+alt+'" /></a>').appendTo('#gallery-ul');
}