2014-03-02 65 views
0

嗨即時通訊使用bx滑塊來顯示我的合作伙伴網站。bxslider - 懸停與克隆元素

PHP代碼如下所示:

$data .= '<div class="twocolor"><a href="'.$client['link'].'" class="thumbnail"> 
<img class="bw img-responsive" src="'.$client['image_bw'].'" alt=""> 
<img class="color img-responsive" style="display:none;" src="'.$client['image'].'" alt=""> 
</a></div>'; 

Basicly即時調節兩個標誌 - 顏色和BW。在懸停我更改圖像不透明隱藏BW圖片和顯示顏色之一。

這裏是jQuery代碼:

jQuery('.twocolor').hover( 
      function() { 
       jQuery(this).find('img.bw').stop().animate({ 'opacity': '0' }, 400); 
      }, 
      function() { 
       jQuery(this).find('img.bw').stop().animate({ 'opacity': '1' }, 400); 
      }); 

問題是,克隆項目不會影響與這個jQuery功能。我應該怎麼做才能在克隆的元素上工作?

回答

2

嘗試使用on()

$(document).on("mouseenter", ".twocolor", function(e) { 
    jQuery(this).find('img.bw').stop().animate({ 'opacity': '0' }, 400); 
}); 

$(document).on("mouseleave", ".twocolor", function(e) { 
    jQuery(this).find('img.bw').stop().animate({ 'opacity': '1' }, 400); 
}); 
+0

就像一個魅力!謝謝。如果這不是問題,你能告訴我爲什麼這不與.find()? – Charles

+0

由於您的元素已被動態添加到DOM。您需要使用事件委派,以便可以將「懸停」事件附加到這些新創建的元素。您可以在這裏閱讀更多關於活動委派的信息:https://learn.jquery.com/events/event-delegation/ – Felix