我想向用户提示他需要在我的文本字段中输入什么。但是,当我设置该值时,一旦用户单击文本字段,它就不会消失。你怎么能让它消失?
<form action="input_password.htm">
<p>Username:<br><input name="Username" value="Enter username.." type="text" size="20" maxlength="20"></p>
</form>
使用一点 JavaScript:
<input
value="Enter username..."
onfocus="if (this.value === 'Enter username...') this.value=''" ... />
HTML5 对此有一个很好的属性,称为placeholder
:
<input placeholder="Enter username.." ... />
但旧浏览器不支持此属性。
您需要onFocus
通过 Javascript 将事件附加到输入字段:
<input type="text" onfocus="this.value=''" value="..." ... />
给出提示的最好方法是这样的占位符:
<input.... placeholder="hint".../>
我认为对于您的情况,您的 html 输入简单易行,您可以添加属性标题
<input name="Username" value="Enter username.." type="text" size="20" maxlength="20" title="enter username">
使用 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>
我有同样的问题,我已将此代码添加到我的应用程序中,它对我来说工作正常。
步骤 -1:添加 jquery.placeholder.js 插件
步骤-2:在您所在的区域编写以下代码。
$(function () {
$('input, textarea').placeholder();
});
现在我可以在输入框上看到占位符了!
这正是你想要的
$(document).tooltip({ selector: "[title]",
placement: "top",
trigger: "focus",
animation: false});
<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>
[参考]
如果您的意思是像背景中的文本,我会说您使用带有输入字段的标签并使用 CSS 将其放置在输入上,当然。使用 JS,您可以在输入接收到值时淡出标签,并在输入为空时淡入标签。这样,用户就不可能提交描述,无论是出于偶然还是有意。
定义工具提示文本
<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>