jQuery 回调函数?

发布于 2025-01-07 04:01:30 字数 1117 浏览 1 评论 0原文

我似乎无法让它工作:这是代码:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2025-01-14 04:01:30

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;
               }
       });

    });

};

现在该函数可以调用了

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.

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;
               }
       });

    });

};

Now the function is invokable

电影里的梦 2025-01-14 04:01:30

尝试将函数放入 document.ready 中,即

$(document).ready(function(){ 
   // Your functions here... 
   $("input:text").each(function(e){
     // code goes here...
   });
});

不需要将其包装在另一个大括号/() 中。

Try to put you functions inside document.ready i.e.

$(document).ready(function(){ 
   // Your functions here... 
   $("input:text").each(function(e){
     // code goes here...
   });
});

No need to wrap it inside another braces/().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文