使具有用户交互功能的 jQuery .mouseenter()/.mouseleave() 脚本正常工作时出现问题
首先,对标题表示歉意,我想不出合适的标题。
我不确定为什么下面代码中的 hide() 函数在触发时会在 firebug 中返回错误,我很确定一旦解决了这个缺陷,代码的其余部分将正常工作,任何帮助/建议将不胜感激。
Firebug 控制台错误:
hide is not defined
it-services() it-services (line 396)
time = setTimeout("hide()",3000);
到目前为止我所拥有的代码:
var time;
$("#form").mouseenter(function() {
clearTimeout(time);
$(this).delay(800).animate({
right: 0
}, 2000);
}).mouseleave(function() {
function hide() {
$(this).delay(800).animate({
right: "-325px"
}, 1000);
}
time = setTimeout(hide,3000);
});
非常感谢大家提前提供的帮助,
Dan。
Firstly, apologies for the title, I could not think of a suitable one.
I am unsure as to why the hide() function within the below code comes back erroneous in firebug when triggered, I am pretty sure the rest of the code will work fine once I have ironed this flaw out, any help/suggestions would be appreciated.
Firebug Console error:
hide is not defined
it-services() it-services (line 396)
time = setTimeout("hide()",3000);
Code I have thus far:
var time;
$("#form").mouseenter(function() {
clearTimeout(time);
$(this).delay(800).animate({
right: 0
}, 2000);
}).mouseleave(function() {
function hide() {
$(this).delay(800).animate({
right: "-325px"
}, 1000);
}
time = setTimeout(hide,3000);
});
Thank you all very much for any help in advance,
Dan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在使用
setTimeout
调用hide()
函数后声明它。只需将声明放在setTimeout
调用之前即可。此外,当您将一串代码作为第一个参数传递给
setTimeout
时,它会被eval
编辑。eval
是邪恶的。只需传递函数对象:You're declaring the
hide()
function after you invoke it usingsetTimeout
. Simply put the declaration before thesetTimeout
call.Also, when you pass a string of code as first argument to
setTimeout
, it getseval
ed.eval
is evil. Just pass the function object:新代码中有两个问题
当它在 mouseout 函数内部被调用时。
我觉得如果它是在 mouseover 事件处理函数之外声明的函数会更有意义。这样您就可以从 setTimeOut 以及 mouseout 事件处理程序全局引用它。尝试下面的代码。我相信这应该可以解决问题,或者至少让您领先一步。
There are 2 issues in the new code
when it is being called inside the mouseout function.
I feel it would make more sense if it were a function declared outside the mouseover event handling function. That way you can globally reference it from the setTimeOut as well as the mouseout event handler. Try the below code. I believe this should solve the issue, or at least take you a step ahead.