我似乎无法让它发挥作用:以下是代码:
// 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()肯定是在页面加载时调用的,但它不能作为回调函数工作吗?我不完全确定原因,有人可以帮助我吗?
干杯
答案 0 :(得分:2)
clearText未定义,它不是函数。你正在调用它,因为你认为你正在定义它。那就是函数赋值结束时的()是什么。
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;
}
});
});
};
现在该函数可以调用
答案 1 :(得分:0)
尝试将函数放在document.ready中。
$(document).ready(function(){
// Your functions here...
$("input:text").each(function(e){
// code goes here...
});
});
无需将其包装在另一个大括号/()中。