2016-04-16 255 views
0

鼠標功能不能支持不支持懸停功能?

$("div.container1:has('#image')").hover(function() { 
     console.log("Success hover"); 
}); 

,這是我的div類的div類

<div class="container1"> 
    <img src="img/1920/blue.jpg" id="imageId"/> 
</div> 

功能的點擊操作

$(document).ready(function() { 
    $(".container1").click(function(e) { 
     e.preventDefault(); 
      var x = e.pageX - this.offsetLeft; 
     var y = e.pageY - this.offsetTop; 

     var img = $('<button>'); 
     img.css('top', y); 
     img.css('left', x); 
     img.css('position', 'absolute'); 
     img.attr('type', 'button'); 
     img.attr('id', 'image'); 
     img.css('z-index', 1); 
     img.attr('class', 'btn btn-info btn-lg'); 
     $(this).attr('data-toggle','modal'); 
     $(this).attr('data-target','#myModal'); 
     img.attr('data-toggle','modal'); 
     img.attr('data-target','#myModal'); 
     console.log("Mouse action Start"); 
     img.appendTo('.container1'); 
     /*$(this).removeClass('.container1');*/ 
     console.log("Mouse action End"); 
     $(this).removeClass('<button>'); 


    }); 


}); 
+0

你可以更清楚,你面對什麼確切的問題? – Sumeet

+0

我有背景圖片。我嘗試用熱點type.so現在我有很多的按鈕和一個背景圖像。我需要按鈕懸停功能 –

回答

0

這是因爲.hover有兩個參數。鼠標進入時的回調,以及它離開時的回調。

另外,要補充的DOM元素,然後添加點擊處理程序?如果是這樣,你一定需要使用.on()

。對()用於被動態添加到DOM元素。 你可能需要做.on('mouseenter', [callback]).on('mouseleave', [callback])而非.hover()。只是fyi。

0

選擇作品的另一種方式,jsbin

$("div.container1 #imageId").hover(function() { 
      console.log("Success hover"); 
    }); 
+0

我也嘗試,但不能迴應 –