簡體   English   中英

jQuery事件在Ajax分頁后無法正常工作

[英]Jquery Event not working after Ajax Pagination

下面是我正在使用的代碼,我一直在使用.mouseenter(function(){沒問題...我一直在閱讀,當您使用.live()時,它將事件應用於現有和新的dom元素,但對於由於某種原因,該函數現在根本無法正常工作,我的語法有問題嗎?

    $(document).ready(function(){
    $("div#rows div.row div:nth-child(1)",this).nextUntil('nth-child(2)').on("mouseenter", function() {
        $('img',this).stop().animate({"bottom":"0px"}, "fast"); 
    });
    $('div',this).off("mouseleave").on("mouseleave", function() {
        $('img',this).stop().animate({"bottom":"94px"}, "fast");    
    });

});

$(document).ready(function(){
    $("div#rows div.row div:nth-child(3)",this).nextUntil('nth-child(4)').on("mouseenter", function() {
        $('img',this).stop().animate({"bottom":"0px"}, "fast"); 
    });
    $('div',this).off("mouseleave").on("mouseleave", function() {
        $('img',this).stop().animate({"bottom":"99px"}, "fast");    
});

});

更新了代碼以使用.on而不是.live ...動畫正在處理第一組帖子,但是ajax分頁加載的所有內容仍不會觸發該事件...

<div id="content">

    <div id="rows">


<div class="row row-1">    
    <div class="loop-0 post-66">
        <div class="post_data">
            <div class="icons_right">
                <a type="readmore" href="http://samplewebsite.com/?p=66" rel="bookmark" title="Permanent Link to Salina Cruz | Mexico"><img src="http://samplewebsite.com/wp-content/themes/sample_website/images/pop_out_icon.png" alt="pop out icon" type="readmore" /></a>
            </div>
            <h1 class="post_title">
                <a href="http://samplewebsite.com/?p=66" rel="bookmark" title="Permanent Link to Salina Cruz | Mexico">
                Salina Cruz | Mexico</a>
            </h1>
            <h2 class="post_snippet">Lorem ipsum dolor</h2>
            <p class="post_date">May 13th, 2012 by <a href="http://samplewebsite.com/?author=1" title="Posts by admin" rel="author">admin</a></p>
        </div> <!-- post_data //-->
        <a href="http://samplewebsite.com/?p=66" rel="bookmark" title="Permanent Link to Salina Cruz | Mexico"><img width="1600" height="1200" src="http://samplewebsite.com/wp-content/uploads/2012/05/surfer3.jpg" class="attachment-post-image wp-post-image" alt="surfer3" title="surfer3" /></a>
    </div>  <!-- 1 //-->

    <div class="loop-1 post-62">
        <div class="post_data">
            <div class="icons_right">
                <a type="readmore" href="http://samplewebsite.com/?p=62" rel="bookmark" title="Permanent Link to Wakeboard Rails | Quick Silver"><img src="http://samplewebsite.com/wp-content/themes/sample_website/images/pop_out_icon.png" alt="pop out icon" type="readmore" /></a>
            </div>
            <h1 class="post_title">
                <a href="http://samplewebsite.com/?p=62" rel="bookmark" title="Permanent Link to Wakeboard Rails | Quick Silver">
                Wakeboard Rails | Quick Silver</a>
            </h1>
            <h2 class="post_snippet">Lorem ipsum dolor</h2>
            <p class="post_date">May 13th, 2012 by <a href="http://samplewebsite.com/?author=1" title="Posts by admin" rel="author">admin</a></p>
        </div> <!-- post_data //-->
        <a href="http://samplewebsite.com/?p=62" rel="bookmark" title="Permanent Link to Wakeboard Rails | Quick Silver"><img width="1280" height="1024" src="http://samplewebsite.com/wp-content/uploads/2012/05/wakeboard1.jpg" class="attachment-post-image wp-post-image" alt="wakeboard1" title="wakeboard1" /></a>
    </div>  <!-- 1 //-->

    <div class="loop-2 post-59">
        <div class="post_data">
            <div class="icons_right">
                <a type="readmore" href="http://samplewebsite.com/?p=59" rel="bookmark" title="Permanent Link to Jeremy Johnston | Florida"><img src="http://samplewebsite.com/wp-content/themes/sample_website/images/pop_out_icon.png" alt="pop out icon" type="readmore" /></a>
            </div>
            <h1 class="post_title">
                <a href="http://samplewebsite.com/?p=59" rel="bookmark" title="Permanent Link to Jeremy Johnston | Florida">
                Jeremy Johnston | Florida</a>
            </h1>
            <h2 class="post_snippet">Lorem ipsum dolor</h2>
            <p class="post_date">May 13th, 2012 by <a href="http://samplewebsite.com/?author=1" title="Posts by admin" rel="author">admin</a></p>
        </div> <!-- post_data //-->
        <a href="http://samplewebsite.com/?p=59" rel="bookmark" title="Permanent Link to Jeremy Johnston | Florida"><img width="1280" height="1024" src="http://samplewebsite.com/wp-content/uploads/2012/05/surfer6.jpg" class="attachment-post-image wp-post-image" alt="surfer6" title="surfer6" /></a>
    </div>  <!-- 1 //-->

    <div class="loop-3 post-56">
        <div class="post_data">
            <div class="icons_right">
                <a type="readmore" href="http://samplewebsite.com/?p=56" rel="bookmark" title="Permanent Link to Caribbean Waters | Duck Dive"><img src="http://samplewebsite.com/wp-content/themes/sample_website/images/pop_out_icon.png" alt="pop out icon" type="readmore" /></a>
            </div>
            <h1 class="post_title">
                <a href="http://samplewebsite.com/?p=56" rel="bookmark" title="Permanent Link to Caribbean Waters | Duck Dive">
                Caribbean Waters | Duck Dive</a>
            </h1>
            <h2 class="post_snippet">Lorem ipsum dolor</h2>
            <p class="post_date">May 13th, 2012 by <a href="http://samplewebsite.com/?author=1" title="Posts by admin" rel="author">admin</a></p>
        </div> <!-- post_data //-->
        <a href="http://samplewebsite.com/?p=56" rel="bookmark" title="Permanent Link to Caribbean Waters | Duck Dive"><img width="1280" height="960" src="http://samplewebsite.com/wp-content/uploads/2012/05/girl2.jpg" class="attachment-post-image wp-post-image" alt="girl2" title="girl2" /></a>
    </div>  <!-- 1 //-->

添加了html:\\

要以委派偵聽器的身份進行此操作,第一個選擇器應為Ajax內容的祖先。 進行ajax調用時,永遠不會破壞的東西。

$('#neverDestroyed').on('mouseenter', '.actualTarget', function() {
  // stuff happens when .actualTarget has mouse enter it
});

如果我說實話,代碼有點“太長;沒看過”,但是上面的代碼在應用到您自己的選擇器時應該可以解決。

仍然不確定自己要做什么,但是您只需要一個document.ready,而選擇第一個孩子直到第二個孩子等等我都無法理解。為什么不將其應用於所有對象,如果動畫值發生變化,請使用一組值,並根據元素索引,ID或其他內容獲得正確的值。

$(document).ready(function(){
    var elms = $("#rows > .row").children();
    var ani = ['94', '94', '99', '99'];

    $(document).on({
        mouseenter: function(e) {
            $('img', e.target).stop().animate({"bottom":"0px"}, "fast"); 
        },
        mouseleave: function(e) {
            var i = $(this).index();
            $('img', e.target).stop().animate({"bottom": ani[i]}, "fast");
        }
    }, elms);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM