自定义函数上textarea的jQuery加载事件

时间:2011-08-07 19:54:54

标签: jquery load textarea

尝试使用自定义函数,其中文本位于页面加载的输入中,以便将其与函数的其余部分分组,其中文本在输入中创建“信息”,如文本。我试过了,.load().bind('load').ready()我没有得到任何结果。只是看看是否可能有解决方案来解决这个问题。

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

}

1 个答案:

答案 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');
        } 
    });

};