jQuery Cycle Plugin (zero element?)

为君一笑 提交于 2019-12-17 19:29:16

问题


I used AJAX to populate a div and then, cycle plugin it !

here is the page, and here is the code calling cycle:

<script type="text/javascript">
    $(function() {
        $("#photoviewer").load("photo-list.shtml #ani");
        alert ('photo loaded!');
        $(document).ready(function() {
            $('#ani').cycle({ fx:      'turnDown',
                              speed:   'fast',
                              timeout: 0,
                              pager:   '#nav' });
            alert('done!');     
        });
    });
</script> 

I get this error : [cycle] terminating; zero elements found by selector, but with firebug i SEE the image list as where it should be...

so i am clueless, any help welcome !

*** take note, the error console (warning section) is going crazy !


回答1:


Instead of initializing cycle in $(document).ready(), do it in a load callback so that it doesn't execute until your ajax call has completed and built the #ani div:

    $(function() {
        $("#photoviewer").load("photo-list.shtml #ani", function() {
            $('#ani').cycle({ fx:      'turnDown',
                              speed:   'fast',
                              timeout: 0,
                              pager:   '#nav' });
        });
    });

As it stands, the cycle plugin initialization is executing before the images have loaded (before the load function has finished).




回答2:


The error just means there is no selector named "#ani" available it seems like the "#ani" is a child node of "#photoviewer" and when the ajax request is complete it then adds "#ani".

After you call:

 $("#photoviewer").load("photo-list.shtml #ani"); 

Try this:

var interval = setInterval(function(){ 
  if(jQuery("#ani").length > 0) {
    clearInterval(interval); 
    jQuery('#ani').cycle({fx:'turnDown', speed:  'fast', timeout: 0, pager:  '#nav'}); 
  }
}, 1);                          


来源:https://stackoverflow.com/questions/1657276/jquery-cycle-plugin-zero-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!