Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 限制JQuery/ajax中的列表选择_Javascript_Jquery_Css_Ajax_Html Lists - Fatal编程技术网

Javascript 限制JQuery/ajax中的列表选择

Javascript 限制JQuery/ajax中的列表选择,javascript,jquery,css,ajax,html-lists,Javascript,Jquery,Css,Ajax,Html Lists,其他问题与此类似,但我的问题是我不知道把信息放在哪里。也就是说,我试图限制从下面脚本中的列表中提取的项目数量。脚本本身工作正常,我只需要将拉取的项目数限制为6个,而不是全部。我觉得这很简单,但我从其他帖子中插入的每一行代码都不适合我。有什么帮助吗 (function($){ $.ajax({ type: "GET", url: "/homepage_photo_slider/PhotoGallery.xml", data

其他问题与此类似,但我的问题是我不知道把信息放在哪里。也就是说,我试图限制从下面脚本中的列表中提取的项目数量。脚本本身工作正常,我只需要将拉取的项目数限制为6个,而不是全部。我觉得这很简单,但我从其他帖子中插入的每一行代码都不适合我。有什么帮助吗

(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);
(函数($){
$.ajax({
键入:“获取”,
url:“/homepage\u photo\u slider/PhotoGallery.xml”,
数据类型:“xml”,
成功:函数(xml){
$(xml).find('img').each(function(){
变量位置='/homepage_photo_slider/';
var url=$(this.attr('src');
var alt=$(this.attr('alt');
$('
  • ').html('').appendTo('#gallery ul'); }); $().piroBox_分机({ 皮罗卢速度:700, bg_α:0.5, 皮罗_卷轴:正确 }); } }); })(jQuery);
    未经测试,但我认为这应该有效。我只更改了注释中标记的一行。在应用
    每个元素之前,我使用slice方法将数组缩减为六个元素:

    (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);
    
    (函数($){
    $.ajax({
    键入:“获取”,
    url:“/homepage\u photo\u slider/PhotoGallery.xml”,
    数据类型:“xml”,
    成功:函数(xml){
    
    $(xml).find('img').slice(0,6).each(function(){/尝试一个for循环,而不是.each()方法:

    var$images=$(xml).find('img');
    对于(变量i=0;i<6;i++)
    {
    var$image=$($images[i])
    变量位置='/homepage_photo_slider/';
    var url=$image.attr('src');
    var alt=$image.attr('alt');
    $('
  • ').html('').appendTo('#gallery ul'); }
    工作得很好!谢谢!
    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');
    }