2011-09-27 109 views
4

可能重复:
How do I make an HTML text box show a hint when empty?HTML文本框输入工具提示?

一个很简单的问题。我想在类似于StackOverflow登录的文本框中放置一个工具提示。我希望文本框的值为单词折扣,并且当用户输入新值时,单词“折扣”从文本框中消失。

我可以自己使用OnBlur,OnChange等事件来做这件事吗(我不知道是哪个)?或者周围有东西已经完成了?

谢谢

+0

它被称为水印。 –

+2

[Many](http://stackoverflow.com/questions/108207/how-do-i-make-an-html-text-box-show-a-hint-when-empty)[similar](http:/ /stackoverflow.com/questions/3429520/how-do-i-get-placeholder-text-in-firefox-and-other-browsers-that-dont-support-th)[questions](http://stackoverflow.com /问题/ 5536346 /占位符换搜索文本框)。 – Alex

+0

谢谢,我不知道这是什么名字。我会稍后检查。 – ivantxo

回答

3

您可以对此使用占位符文本。

<input type="text" id="discount" placeholder="discount" /> 

要使它适用于所有浏览器,您需要使用jquery/javascript。据我所知,它在IE中不起作用。

+0

谢谢。我会稍后检查。 – ivantxo

6

正如rudeovski所说,您可以在现代浏览器中使用占位符。
但是对于较老的,你需要一个javascript/jQuery后备。

这里是一个很好的例子:

http://uniquemethod.com/html5-placeholder-text-with-modernizr-and-jquery-fallback

这将会被自动为您生成占位符回退。

您只需写:

<input type="text" id="discount" placeholder="discount" /> 

Modernizr的jQuery的合并将完成剩下的为您服务。
希望这个帮助:)

+0

谢谢。我会稍后检查。 – ivantxo

相关问题