如何使jQuery 1.7 .on()悬停?

前端之家收集整理的这篇文章主要介绍了如何使jQuery 1.7 .on()悬停?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在悬停状态下动态创建元素时遇到问题.当我将鼠标悬停在新创建的html元素上时,它不起作用.

这是我的HTML代码

  1. <button id="create">create new button</button>
  2. <button class="hover">hover me</button>
  3. <div></div>

jQuery的:

  1. var createBtn = $("#create");
  2.  
  3. createBtn.click(function() {
  4. $('div').append('<button class="hover">new hover button</button');
  5. return false;
  6. });
  7.  
  8.  
  9. $('.hover').hover(function() {
  10. alert('you hovered the button!');
  11. },function() {
  12. alert('you removed the hover from button!');
  13. });

我甚至试过这段代码

  1. $('.hover').on({
  2. mouseenter : function() {
  3. alert('you hovered the button!');
  4. },mouseleave : function() {
  5. alert('you removed the hover from button!');
  6. }
  7.  
  8. });

如图所示http://api.jquery.com/on/,但仍然没有运气.
这里也是演示:http://jsfiddle.net/BQ2FA/

解决方法

这不是正确的语法.

使用它来监听动态创建的“.hover”元素的事件:

  1. $(document).on('mouseenter','.hover',function(){
  2. alert('you hovered the button!');
  3. }).on('mouseleave',function() {
  4. alert('you removed the hover from button!');
  5. });

猜你在找的jQuery相关文章