简体   繁体   English

在ajax加载的内容上加载jquery循环

[英]Load jquery cycle on ajax loaded content

I'm trying to load the plugin Jquery Cycle on an ajax loaded content, but I get this error and it doesn't seem to work: 我正在尝试在ajax加载的内容上加载插件Jquery Cycle,但是我收到此错误并且它似乎不起作用:

"[cycle] terminating; zero elements found by selector " “[循环]终止;选择器找到的零元素”

This is the loader.js: 这是loader.js:

$(document).ready(function(){

// load home when the page loads
$("#content").load("home.html", function(){
  $(this).fadeIn("slow");
});

// load artworks page
$("#artworks > a").click(function(){
    $("#content").hide();
    $("#content").load("artworks.html", function(){
        $(this).fadeIn("slow");
    });
});

    // load projects page
    $("#artworks ul li a").click(function(){
        $("#content").hide();
        $("#content").load("project.html", function(){
            $(this).fadeIn("slow");
        });
    });

    // load single project page         
    $("#project_page").live("click", function(){
        $("#content").hide();
        $("#content").load("project.html", function(){
            $(this).fadeIn("slow");
        });
    });

        // load single project page         
        $("#project_slider").live("click", function(){
            $("#content").hide();
            $("#content").load("project_inside.html", function(){
                $(this).fadeIn("slow");
            });
        });

        // back to projects page        
        $(".back").live("click", function(){
            $("#content").hide();
            $("#content").load("project.html", function(){
                $(this).fadeIn("slow");
            });
        }); 

// load prensa page
$("#prensa_nav").click(function(){
    $("#content").hide();
    $("#content").load("prensa.html", function(){
      $(this).fadeIn("slow");
    });
});

// load contacto page
$("#contacto_nav").click(function(){
    $("#content").hide();
    $("#content").load("contacto.html", function(){
      $(this).fadeIn("slow");
    });
}); 

// SIDEBAR MENU

// Add class of drop if item has sub-menu
$('ul.submenu').hide().parent('li').addClass('drop');

// open submenu
$('.drop').click(function(){
   $('.submenu',this).slideToggle();
});

//hide submenu when you click other main items            
 $('#menu>li').click(function(){
     if(!$(this).hasClass('active')){
         $('.active .submenu').slideUp();
     }
});

//hide submenu when you click other main items -diego           
 $('#menu li#prensa_nav').click(function(){
     if(!$(this).hasClass('active')){
         $('.submenu').slideUp();
     }
});

//hide submenu when you click other main items -diego           
 $('#menu li#contacto_nav').click(function(){
     if(!$(this).hasClass('active')){
         $('.submenu').slideUp();
     }
});

// active menu item    
$('#menu li').click(function(event) {
    $('#menu li').removeClass('active');
    $(this).addClass('active');      
    event.stopPropagation();
});

// active menu item -diego  
$('#menu li ul li').click(function(event) {
    $('#menu li#artworks ').addClass('active');     
    event.stopPropagation();
});  

});

$(window).load(function() {

// Slider  
$("#slideshow").css("overflow", "hidden");

$("ul#slides").cycle({
    fx: 'fade',
    pause: 1,
    prev: '#prev',
    next: '#next'
    });

$("#slideshow").hover(function() {
    $("ul#nav").fadeIn();
},

function() {
    $("ul#nav").fadeOut();
});

});

Here it's the plugin: http://malsup.github.com/jquery.cycle.all.js 这是插件: http//malsup.github.com/jquery.cycle.all.js

How can I fix this error to load the plugin? 如何修复此错误以加载插件?

i'm following this tutorial where it does that way... also I did it with a clean HTML and it worked!! 我正在按照本教程的方式进行操作......我也用干净的HTML做了它,它工作了!! the problem is when I integrate it with the ajax loaded content: http://line25.com/tutorials/build-a-simple-image-slideshow-with-jquery-cycle 问题是当我将它与ajax加载的内容集成时: http//line25.com/tutorials/build-a-simple-image-slideshow-with-jquery-cycle

Why don't you just do $("#slides") ? 你为什么不做$("#slides") You should only have one element with an ID of "slides" on your page. 您的页面上只应有一个ID为“幻灯片”的元素。 If you need to select multiple objects, consider using a css class and then select with $('.class-name') . 如果需要选择多个对象,请考虑使用css类,然后选择$('.class-name')

I think the problem is that you're trying to initialize the cycle on an empty list, before the data is loaded into the list via AJAX. 我认为问题是你在通过AJAX将数据加载到列表之前尝试在空列表上初始化循环。 You should, instead, initialize the cycle after the data is injected into the DOM as list items: 相反,您应该在将数据作为列表项注入DOM后初始化循环:

$('#content').load('home.html', function(){
    $("ul#slides").cycle({
        fx: 'fade',
        pause: 1,
        prev: '#prev',
        next: '#next'
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM