jQuery 回调函数?
我似乎无法让它工作:这是代码:
// Clears the default text on click, but restores if nothing is entered
var clearText = (function(){
$("input:text").each(function () {
var default_value = this.value;
$(this).focus(function(){
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function(){
if(this.value == '') {
this.value = default_value;
}
});
});
$("textarea").each(function () {
var default_value = this.value;
$(this).focus(function(){
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function(){
if(this.value == '') {
this.value = default_value;
}
});
});
})();
$('html').on('keydown', 'body', function(e){
if (e.which == 9)
{
clearText();
}
});
clearText() 肯定在页面加载时被调用,但它不能作为回调函数工作?我不完全确定为什么,有人可以帮助我吗?
干杯
I can't seem to get this to work: Here's the code:
// Clears the default text on click, but restores if nothing is entered
var clearText = (function(){
$("input:text").each(function () {
var default_value = this.value;
$(this).focus(function(){
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function(){
if(this.value == '') {
this.value = default_value;
}
});
});
$("textarea").each(function () {
var default_value = this.value;
$(this).focus(function(){
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function(){
if(this.value == '') {
this.value = default_value;
}
});
});
})();
$('html').on('keydown', 'body', function(e){
if (e.which == 9)
{
clearText();
}
});
clearText() is definitely being called at page load, but then it doesn't work as a callback function? I'm not entirely sure why, can anyone help me out?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
clearText 未定义,它不是一个函数。您按照您认为定义的方式来称呼它。这就是函数赋值末尾的 () 的意思。
现在该函数可以调用了
clearText is undefined, it is not a function. You were calling it as you thought you were defining it. Thats what the () at the end of the function assignment was.
Now the function is invokable
尝试将函数放入 document.ready 中,即
不需要将其包装在另一个大括号/() 中。
Try to put you functions inside document.ready i.e.
No need to wrap it inside another braces/().