嗨,我在下拉导航菜单上使用mouseleave(),这样当用户离开子菜单下拉菜单时,子菜单就会消失,但是它似乎忽略了它并且菜单仍然存在。有什么想法吗?这是网站和代码:
http://www.maiagifts.co.uk/about-us/info_1.html
$(document).ready(function() {
$('#newcats li').addClass('parentitem');
$('.newsubs li').removeClass('parentitem');
$('.newsubs').hide();
$('.parentitem').hover(
function(){
$(this).children('.newsubs').show();
$(this).siblings('.parentitem').children('.newsubs').hide();
});
//problem is here//
$('.newsubs').mouseleave(
function(){
$(this).hide();
});
//problem is here//
});
尝试使用.on
$('.newsubs').on('mouseleave', function(){
$(this).hide();
});
如果您使用的是低于 1.7.1 的 jquery 版本,请使用 .live()
你试过吗:
$('.newsubs').mouseleave(
function(){
$('.newsubs').hide();
});
这会达到你想要的吗?
如果您查看要链接到的站点的开发控制台,您将看到一堆错误消息,其中一个(重复)正在"Uncaught TypeError: Object #<Object> has no method 'mouseleave'"
,因此上述附加处理程序的代码永远不会执行。
.on
也没有定义,我猜它们都是您拥有的第一条错误消息的结果,即 Uncaught TypeError: Cannot read property 'guid' of undefined
在 jquery 的第 91 行。
换句话说,jQuery脚本不是以完整的长度执行的,并且从未定义mouseleave
和其他方法(如on
)。
我将hover()方法更改为.mouseover()并开始工作。我想知道是否因为我将 hover() 与一个函数仅用于鼠标悬停部分,jquery 已经决定它有一个空的鼠标离开,所以忽略了第二个。只是一个猜测。