开发者

Is there a JQuery plugin for displaying default text in a text field?

When I hit the page, the text box says "Search...", in gray font.

Then, I click the textbox. Then, th开发者_StackOverflow中文版e "search..." disappears. And I can type.


What you're looking for is called a watermark effect. You can find appropriate jQuery plugins here.


This is typically called "default text" and yes, there's a plugin for it.

JQuery Default Text Plugin

I've tried rolling my own default text plugin before but there were a lot of edge cases to contend with. For example, the plugin must know what its default text is in order to clear said default text if the text field is left "blank", otherwise the form will try to submit that default text. Basically, what I'm saying is use the plugin as it should cover these edge cases for you.


I offer my own solution to this: http://tholljos.lu/mike/files/watermark.zip

jQuery(selector).watermark('here be text...','cssClass');

'here be text' will be your watermark text. 'cssClass' is a css class that is added to the input field in order to style the watermark text.

AFAIAC this covers pretty much every edge case I have encountered so far. Of course, you can use the other plugins if you want, but I find my solution simple and effective.


$("textinput_id").focus(function(){
    $(this).text("");
    });

$("textinput_id").blur(function(){
    $(this).text("Search...");
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜