HTML输入字段提示

时间:2011-06-08 14:08:49

标签: html textfield hint

我想向用户提供他需要输入我的文本字段的提示。但是,当我设置值时,一旦用户单击文本字段,它就不会消失。你怎么能让它消失?

<form action="input_password.htm">
  <p>Username:<br><input name="Username" value="Enter username.." type="text" size="20" maxlength="20"></p>
</form>

9 个答案:

答案 0 :(得分:94)

有点javascript

<input value="Enter username.." onfocus="if (this.value == 'Enter username..') this.value=''" ... />

HTML5有一个很好的属性,称为占位符

<input placeholder="Enter username.." ... />

但旧版浏览器不支持以上版本。

答案 1 :(得分:23)

您需要通过Javascript:

onFocus事件附加到输入字段
<input type="text" onfocus="this.value=''" value="..." ... />

答案 2 :(得分:15)

提供提示的最佳方式是这样的占位符:

<input.... placeholder="hint".../>

答案 3 :(得分:12)

我认为对于您的情况,您可以轻松简单地输入html 可能会添加属性标题

<input name="Username" value="Enter username.." type="text" size="20" maxlength="20" title="enter username">

答案 4 :(得分:9)

使用HTML5,您现在可以使用占位符属性,如下所示:

<form action="demo_form.asp">
  <input type="text" name="fname" placeholder="First name"><br>
  <input type="text" name="lname" placeholder="Last name"><br>
  <input type="submit" value="Submit">
</form> 

http://www.w3schools.com/tags/att_input_placeholder.asp

答案 5 :(得分:3)

这正是你想要的

&#13;
&#13;
$(document).tooltip({ selector: "[title]",
                              placement: "top",
                              trigger: "focus",
                              animation: false}); 
&#13;
<form id="form">
    <label for="myinput1">Browser tooltip appears on hover but disappears on clicking the input field. But this one persists while user is typing within the field</label>
    <input id="myinput1" type="text" title="This tooltip persists" />
    <input id="myinput2" type="text" title="This one also" />
</form>
&#13;
&#13;
&#13;

[ref]

答案 6 :(得分:2)

我遇到了同样的问题,我已将此代码添加到我的应用程序中,并且它的工作正常。

步骤-1:添加了jquery.placeholder.js插件

步骤-2:在您所在的区域编写以下代码。

$(function () {
       $('input, textarea').placeholder();
});

现在我可以在输入框上看到占位符了!

答案 7 :(得分:0)

如果你的意思是背景中的文字,我会说你使用带有输入字段的标签,当然是使用CSS将它放在输入上。使用JS,当输入接收值时淡出标签,并在输入为空时淡入标签。通过这种方式,用户无法通过意外或意图提交描述。

答案 8 :(得分:-4)

定义工具提示文本

<input type="text" id="firstname" name="firstname" tooltipText="Type in your firstname in this box"> 

初始化并配置脚本

<script type="text/javascript">
var tooltipObj = new DHTMLgoodies_formTooltip();
tooltipObj.setTooltipPosition('right');
tooltipObj.setPageBgColor('#EEE');
tooltipObj.setCloseMessage('Exit');
tooltipObj.initFormFieldTooltip();
</script>