简体   繁体   English

悬停在图块上时,JQuery.hover()无法正常工作

[英]JQuery.hover() not working when hovering over tile

I'm trying to add some functionality when hovering over a 2048 tile. 悬停在2048磁贴上时,我试图添加一些功能。 Each tile of value n has a class 'tile-n'. 值n的每个图块都有一个“ tile-n”类。

For the basic 'tile-2' tile, I have the hover functionality: 对于基本的“ tile-2”图块,我具有悬停功能:

  <script>
    $( ".tile-2" ).hover(
      function(){
        alert("in!");
      }, function() {
        alert("out!");
      }
    );
  </script>

But nothing is happening and I don't believe the hover is being registered and I'm not sure why. 但是没有任何反应,我不认为悬停已经注册,我不确定为什么。

My implementation can be seen at: http://kpscript.github.io/PCN-Embark-2048/ 可以在以下位置查看我的实现: http//kpscript.github.io/PCN-Embark-2048/

The 'tile-2' div can be seen at: html.body.container.game-container.tile-container.tile-2. 可以在html.body.container.game-container.tile-container.tile-2中看到“ tile-2” div。

Of course, I plan to do more non-trivial things with hover, but for now I can't even get the alert to show. 当然,我计划使用悬停功能来进行更多不平凡的事情,但是现在我什至无法显示警报。

I think you load html using ajax, this should work 我认为您使用ajax加载html,这应该工作

<script>
$( "body" ).on('hover','.tile-2',
  function(){
    alert("in!");
  }, function() {
    alert("out!");
  }
);

;) ;)

Try like this, It works for me... 像这样尝试,它对我有用...

    $("body").on('mouseenter', '.tile-2', function () {
        alert("in!");
    }).on('mouseleave', '.tile-2', function () {
        alert("out!");
    });

You are trying to bind the event to the element before the element is rendered on the page. 您正在尝试将事件绑定到元素,然后再在页面上呈现该元素。 Wait until its loaded. 等待其加载。

$(document).ready({

  $( ".tile-2" ).hover(
      function(){
        alert("in!");
      }, function() {
        alert("out!");
      }
    );

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM