2016-04-16 148 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

我也尝试,但不能回应 –