繁体   English   中英

jQuery悬停事件未触发

[英]jQuery hover event not firing

代码示例如下:

(function (window, document) {
$('.rating_title_container').parents('.item_row_def').hover(function() {
          setTimeout(function() {
              system.console('Worked');
          }, 1000);
        });
 })(window, document);  

我对JS,jQuery非常陌生。 谁能解释我在这里缺少什么? 将代码发布到http://jsfiddle.net/p7gBy/5/

的HTML

  <table>
    <thead>
      <tr>
        <th class="item_row_def" onclick="sort(3);">
          <table class="col-header">
            <tbody>
              <tr>
                <td>
                  <h2 class="header_title rating_title_container">Rating</h2>
                </td>
              </tr>
            </tbody>
          </table>
        </th>
      </tr>
    </thead>
  </table>

尝试按照以下假设在文档准备就绪时调用代码:

jQuery(function (window, document) {
$('.rating_title_container').parents('.item_row_def').hover(function() {
      setTimeout(function() {
          system.console('Worked');
      }, 1000);
    });
});

并且同时您已超出}); 最后,这会引发错误

您需要将事件处理程序绑定到准备就绪的文档中,如下所示(将上面的代码替换为此,然后查看):

$(document).ready(function(){
    $('.rating_title_container').parents('.item_row_def').hover(function() {
      setTimeout(function() {
          system.console('Worked');
      }, 1000);
    });
});

试试这个代码:

(function (window, document) {
$('.rating_title_container').parents('.item_row_def').hover(function() {
          setTimeout(function() {
              alert('Worked');
          }, 1000);
        });

})(window, document); 

检查小提琴http://jsfiddle.net/AXepU/

您必须将函数doc ready封装起来,然后一切正常:

$(function(){ // <----------------------------try enclosing within this from here
   (function (window, document) {
      $('.rating_title_container').parents('.item_row_def').hover(function() {
        setTimeout(function() {
            alert('Worked');
        }, 1000);
      });
   })(window, document); 
}); //<---------------------------------------- to here.

大多数事件应写在document ready处理程序内。

这个:

$(document).ready(function() {
   // Handler for .ready() called. 
});

和这个:

$(function() {
    // Handler for .ready() called.
});

都一样 第二个是doc ready处理程序的较短版本。

阅读有关.ready()处理程序的更多信息

暂无
暂无

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

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