2011-08-07 42 views
0

尝试在文本位于页面加载的输入中的情况下尝试使用自定义函数,以便将文本与文本在输入中创建“信息”类似文本的其余部分分组。我试过,​​,.bind('load'),.ready(),我没有得到任何东西。只是看看是否有可能解决这一路上的碰撞问题。自定义函数上的textarea的jQuery加载事件

//removed some of the validation code for simplicity. 

/* 
to set a grey text infomation into a input field 
*/ 
$.fn.greyInfo = function (text) 
{ 

    //troubled code - start 
    $(this).load(function() 
    { 
     $(this).val(text); 
     $(this).css('color', 'grey'); 
    }); 
    //troubled code - end 

    $(this).blur(function() 
    { 
     if($(this).val() == "") 
     { 
      $(this).val(text); 
      $(this).css('color', 'grey'); 
     } 
    }); 

    $(this).focus(function() 
    { 
     if($(this).val() == text) 
     { 
      $(this).val(""); 
      $(this).css('color', 'black'); 
     } 
    }); 

} 
+0

发现我的回答 '$(本).blur();' 把这个在自定义函数 – thebtm

+0

结束时,我不认为加载事件在这方面作为触发$(本)我认为它已经在你的调用之前触发,尝试在你使用某个对象的函数后触发它。 –

回答

2

答案很简单,你不需要调用该函数加载,因为它已经存在。

$.fn.greyInfo = function(text) 
{ 

// you also don't need to reference $(this) as the jquery object is implied 
     this.val(text); 
     this.css('color', 'grey'); 


    $(this).blur(function() 
    { 
     if($(this).val() == "") 
     { 
      $(this).val(text); 
      $(this).css('color', 'grey'); 
     } 
    }); 

    $(this).focus(function() 
    { 
     if($(this).val() == text) 
     { 
      $(this).val(""); 
      $(this).css('color', 'black'); 
     } 
    }); 

}; 
+0

我测试过了,它确实有效。 – rlemon

+0

非常感谢。 – thebtm