为什么萤火虫说我的函数调用是“未定义的"? [英] Why does firebug say my function call is "undefined"

查看:91
本文介绍了为什么萤火虫说我的函数调用是“未定义的"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含子菜单的导航菜单.悬停时,我希望子菜单在第二次延迟后显示.标记为更多"类的菜单项包含子菜单.

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.

问题是我的一个名为hoverCheck()的函数在被调用时返回为未定义状态.但是我不知道为什么.

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.

这是我的代码:

$(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');
 });

}

推荐答案

您会收到一条错误消息,因为该函数未在全局范围内定义,相反,您应该这样做:

You're getting an error because that function isn't defined in the global scope, instead you should do this:

setTimeout(hoverCheck, 1000);

作为一般规则,请尽量避免将字符串传递给setTimeout(),直接为其提供函数引用-否则,您可能会遇到范围界定问题,就像这样:)

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 :)

这篇关于为什么萤火虫说我的函数调用是“未定义的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆