为什么 firebug 说我的函数调用是“未定义”
我有一个包含子菜单的导航菜单。悬停时,我希望子菜单在第二次延迟后显示。标有“更多”类别的菜单项包含子菜单。
问题是,我的一个名为“hoverCheck()”的函数在调用时返回为“未定义”。但我不明白为什么。
这是我的代码:
$(document).ready(function() {
navigation();
});
function navigation() {
var moreMenu = $('.nav li.more');
var hovering;
function hoverCheck() {
hovering = 'hover';
openMenu();
}
function openMenu() {
if(hovering == 'hover') {
$(this).children('ul').slideDown('fast');
}
}
moreMenu.mouseenter(function() {
setTimeout("hoverCheck()",1000);
});
moreMenu.mouseleave(function() {
hovering = null;
$(this).children('ul').slideUp('fast');
});
}
I have a navigation menu which contain sub menus. On hover I want the sub menus to show after a second delay. Menu items marked with a class of "more" contain sub menus.
Problem is that one of my functions called hoverCheck() is coming back as undefined when it's called. But I can't figure out why.
Here's my code:
$(document).ready(function() {
navigation();
});
function navigation() {
var moreMenu = $('.nav li.more');
var hovering;
function hoverCheck() {
hovering = 'hover';
openMenu();
}
function openMenu() {
if(hovering == 'hover') {
$(this).children('ul').slideDown('fast');
}
}
moreMenu.mouseenter(function() {
setTimeout("hoverCheck()",1000);
});
moreMenu.mouseleave(function() {
hovering = null;
$(this).children('ul').slideUp('fast');
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到错误是因为该函数未在全局范围内定义,您应该这样做:
作为一般规则,尽量避免将字符串传递给
setTimeout()
,而是将其传递给 setTimeout()直接函数引用 - 否则你可能会遇到范围问题,就像这样:)You're getting an error because that function isn't defined in the global scope, instead you should do this:
As a general rule, try to always avoid passing a string to
setTimeout()
, give it a function reference directly - otherwise you may run into scoping issues, just like this :)setTimeout 函数采用表达式作为第一个参数,而不是函数的名称。如果你
用
它替换可能会成功。
The setTimeout function takes an expression as the first argument, not the name of a function. If you replace
with
that might do the trick.