代码之家  ›  专栏  ›  技术社区  ›  coldpumpkin

无限滚动-jQuery无法处理新获取的元素

  •  1
  • coldpumpkin  · 技术社区  · 11 年前

    基本上,我在我的WordPress博客上实现了无限滚动(.com),下面是代码:

    HTML格式

    <div id="photos" class="display-fix">
        <?php while ( have_posts() ) : the_post(); ?>
        <img src="<?php $url = get_post_thumbnail_id(); $url = wp_get_attachment_image_src($url,'medium'); $url = $url[0]; echo $url; ?>">      
        <?php endwhile; ?>
    </div>
    <div id="pagination" class="clear right">        
        <?php if(function_exists('wpex_pagination')) { wpex_pagination(); } ?>
    </div>
    

    JS文件

    $('#photos').infinitescroll({
        navSelector  : ".next:last", // selector for the paged navigation (it will be hidden) 
        nextSelector : "a.next:last", // selector for the NEXT link (to page 2) 
        itemSelector : "#photos img", // selector for all items you'll retrieve 
        loading: {
            finished: undefined,
            finishedMsg: '',
            img: "<?php echo get_template_directory_uri(); ?>/images/preloader.gif",
            msg: null,
            msgText: '',
            selector: null,
            speed: 'slow',
            start: undefined
        }
    });
    
    $('#photos img').hover(function(){
        alert('Works');
    });
    

    CSS格式

    #photos {
        width: 100%;
        padding: 20px;
    }
    #photos img {
        width: 504px;
        margin: 3px;
        float: left;
    }
    #infscr-loading {
        position: absolute;
        width: 100px;
        height: 100px;
        left: 50%;
        margin-left: -50px;
        bottom: 10px;
    }
    #infscr-loading img {
        width: 100px;
        height: 100px;
    }
    

    当我向下滚动时,当图像弹出时,悬停警告不起作用,它只在显示的第一个图像上起作用!弹出的那些不起作用。

    为什么?我正在使用 $(document).ready(function() { 我还要用别的吗?

    2 回复  |  直到 11 年前
        1
  •  0
  •   VladL Tejas    11 年前

    这是因为悬停事件附加到最初可见的元素,只需处理 infinitescroll :

    $('#photos').infinitescroll({
        navSelector  : ".next:last", // selector for the paged navigation (it will be hidden) 
        nextSelector : "a.next:last", // selector for the NEXT link (to page 2) 
        itemSelector : "#photos img", // selector for all items you'll retrieve 
        loading: {
            finished: registerHover,
            finishedMsg: '',
            img: "<?php echo get_template_directory_uri(); ?>/images/preloader.gif",
            msg: null,
            msgText: '',
            selector: null,
            speed: 'slow',
            start: undefined
        }
    });
    
    registerHover();
    
    function registerHover()
    {
        $('#photos img').hover(function(){
           alert('Works');
        });
    }
    
        2
  •  0
  •   DevlshOne    11 年前

    你需要使用授权。

    更改:

    $('#photos img').hover(function(){
        alert('Works');
    });
    

    $('#photos img').on('mouseover', function(){
        alert('Works');
    });