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>
您需要将事件处理程序绑定到这样一个文档中(用这个替换上面的代码,请参阅):
$(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/
尝试以下假设在文档准备好时调用代码:
jQuery(function (window, document) {
$('.rating_title_container').parents('.item_row_def').hover(function() {
setTimeout(function() {
system.console('Worked');
}, 1000);
});
});
同时,您在末尾超过了});
,这会引发错误
您必须包含函数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()处理程序的更多信息
相关文章:
- 从控制器返回后Ajax启动事件激发
- 触摸启动事件未在iframe iOS 6中启动
- preventDefault之后的重新启动事件
- 如何获取在一系列事件中启动事件的元素
- 当窗口打开时,IE9在加载前启动事件
- 我的 JS 代码中的会话启动事件
- 仅当前一个事件完全完成时,才启动事件
- jQuery UI - 如何区分拖动/调整大小启动事件和单击事件
- Javascript “卡住”键(未注册的键启动事件)
- pace.js:“启动”事件未触发
- 在第一次触摸启动事件时禁用所有点击事件是个好主意吗?
- 如何将“拖动启动”事件与 CKEditor 实例分离
- 触摸启动事件上的Javascript触发函数
- 绑定到触摸启动事件,导致自动对焦集中在Mobile Safari中的任何单击上
- 触摸启动事件未在原来隐藏的按钮上工作
- 有没有一种方法可以使触摸启动事件不会触发点击事件
- 检测浏览器窗口是否处于活动状态,并在窗口再次处于活动状态后启动事件[JavaScript]
- 如何检测浏览器是否支持手势启动事件
- 首先启动事件,然后转到url
- 在IE7中无法捕获鼠标启动事件