使用“load()”重新加载页面后,使用“hover()”绑定的事件不会触发
我有两个脚本:一个是用于 AJAX 效果的导航脚本,一个是用于悬停效果的脚本。
<!--Band Images-->
<script type="text/javascript">
$(document).ready(function(band) {
var name = "";
$(".home-roll-box").hover(function() {
name = $(this).attr("id");
$("#image-" + name).stop().show().animate({
opacity: 1
});
}, function() {
name = $(this).attr("id");
$("#image-" + name).stop().animate({
opacity: 0
});
});
});
</script>
<!--/Band Images-->
<!--Navigation-->
<script type="text/javascript">
$.ajaxSetup({
cache: false
});
$('ul.navigation li a').click(function(nav) {
$('ul.navigation li.page_item.current_page_item').removeClass('current_page_item');
$('ul.navigation li.page_item a.active').removeClass('active');
$('#content-wrap').animate({
top: "-2000px"
}, 1000);
var targetPage = $(this).attr('href');
targetPage += " #content";
setTimeout(function() {
$('#content-wrap').load(targetPage, function() {
$('#content-wrap').animate({
top: "0px"
}, 1000);
});
});
$(this).addClass('active');
nav.preventDefault();
});
</script>
<!--/Navigation-->
一旦执行了改变导航的代码,其他代码(使用 hover()
绑定到带区图像的事件处理程序)就无法运行。
有什么想法吗?
These are the two scripts I have: One is a navigation script for an AJAXing effect and one is for a hover effect.
<!--Band Images-->
<script type="text/javascript">
$(document).ready(function(band) {
var name = "";
$(".home-roll-box").hover(function() {
name = $(this).attr("id");
$("#image-" + name).stop().show().animate({
opacity: 1
});
}, function() {
name = $(this).attr("id");
$("#image-" + name).stop().animate({
opacity: 0
});
});
});
</script>
<!--/Band Images-->
<!--Navigation-->
<script type="text/javascript">
$.ajaxSetup({
cache: false
});
$('ul.navigation li a').click(function(nav) {
$('ul.navigation li.page_item.current_page_item').removeClass('current_page_item');
$('ul.navigation li.page_item a.active').removeClass('active');
$('#content-wrap').animate({
top: "-2000px"
}, 1000);
var targetPage = $(this).attr('href');
targetPage += " #content";
setTimeout(function() {
$('#content-wrap').load(targetPage, function() {
$('#content-wrap').animate({
top: "0px"
}, 1000);
});
});
$(this).addClass('active');
nav.preventDefault();
});
</script>
<!--/Navigation-->
Once the code which alters the navigation has executed, the other code (the event handlers bound for the band images using hover()
), fail to run.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
load
正在重新加载 DOM 中包含 < 的部分code>.home-roll-box 元素,您会发现通过load
调用插入的新元素不会将事件处理程序绑定到它们。hover
在幕后使用bind
;它仅将处理程序绑定到 DOM 中当前的元素。未来的元素将不会将这些处理程序绑定到它们。使用 live 代替;它将处理程序绑定到与选择器匹配的所有当前和未来元素(您还应该查看 delegate,完成在 jquery 中绑定事件处理程序的方法集)。If your
load
is re-loading the part of your DOM which contain your.home-roll-box
elements, you'll find the new elements inserted by yourload
call will not have the event handlers bound to them.hover
usesbind
behind the scenes; which only binds handlers to elements currently in the DOM. Future elements will not have those handlers bound to them. Use live instead; which binds handlers to all current, and future elements that match the selector (you should also look at delegate, to complete the set of ways-to-bind-event-handlers-in-jquery).感谢@Matt 的回答,但他的代码中有一个非常轻微的错误。
功能齐全,具有以下功能:
Thanks to @Matt for the answer, but there was a very slight error in his code.
Fully functioning with the following: