我想在我的网页上的搜索框显示字“搜索”在灰色斜体。当框接收到焦点时,它看起来就像一个空文本框。如果其中已经有文本,则应该正常显示文本(黑色,非斜体)。这将帮助我避免混乱的标签。
顺便说一句,这是一个页面Ajax搜索,所以它没有按钮。
我想在我的网页上的搜索框显示字“搜索”在灰色斜体。当框接收到焦点时,它看起来就像一个空文本框。如果其中已经有文本,则应该正常显示文本(黑色,非斜体)。这将帮助我避免混乱的标签。
顺便说一句,这是一个页面Ajax搜索,所以它没有按钮。
你可以添加和删除一个特殊的CSS类,用JavaScript修改输入值onfocus/onblur:
<input type="text" class="hint" value="Search..."
onfocus="if (this.className=='hint') { this.className = ''; this.value = ''; }"
onblur="if (this.value == '') { this.className = 'hint'; this.value = 'Search...'; }">
然后在CSS中指定一个样式提示类,例如:
input.hint {
color: grey;
}
你可以很容易地有一个框读“搜索”,然后当焦点改变到它有文本被删除。就像这样:
< onfocus =“这个输入。价值=“类型”=“文本”=“搜索”/>
当然,如果你这样做,用户自己的文本将消失时,他们点击。所以你可能想要使用更健壮的东西:
<input name="keyword_" type="text" size="25" style="color:#999;" maxlength="128" id="keyword_"
onblur="this.value = this.value || this.defaultValue; this.style.color = '#999';"
onfocus="this.value=''; this.style.color = '#000';"
value="Search Term">
最好的方法是使用某种JavaScript库(如jQuery或YUI)将JavaScript事件连接起来,并将代码放在外部的.js文件中。
但如果你想要一个快速的解决方案,这是你的内联html解决方案:
<input type="text" id="textbox" value="Search"
onclick="if(this.value=='Search'){this.value=''; this.style.color='#000'}"
onblur="if(this.value==''){this.value='Search'; this.style.color='#555'}" />
更新:增加了要求的着色材料。
使用背景图像来渲染文本:
input.foo { }
input.fooempty { background-image: url("blah.png"); }
然后你所要做的就是检测value == 0并应用正确的类:
<input class="foo fooempty" value="" type="text" name="bar" />
jQuery JavaScript代码如下所示:
jQuery(function($)
{
var target = $("input.foo");
target.bind("change", function()
{
if( target.val().length > 1 )
{
target.addClass("fooempty");
}
else
{
target.removeClass("fooempty");
}
});
});
你想要这样分配给onfocus:
if (this.value == this.defaultValue)
this.value = ''
this.className = ''
这是onblur:
if (this.value == '')
this.value = this.defaultValue
this.className = 'placeholder'
(如果你愿意,你可以使用一些更聪明的东西,比如框架函数,来进行类名切换。)
用一些CSS像这样:
input.placeholder{
color: gray;
font-style: italic;
}
这就是所谓的文本框水印,它是通过JavaScript完成的。
http://naspinski.net/post/Text-Input-Watermarks-using-Javascript- (IE-Compatible) . aspx
或者如果你使用jQuery,一个更好的方法:
http://digitalbush.com/projects/watermark-input-plugin/ 或code.google.com/p/jquery-watermark
当页面第一次加载时,让搜索出现在文本框中,如果你想要它是灰色的。
当输入框接收到焦点时,选择搜索框中的所有文本,这样用户就可以开始输入,这会在此过程中删除所选文本。如果用户想要第二次使用搜索框,这也会很好地工作,因为他们不需要手动突出显示之前的文本来删除它。
<input type="text" value="Search" onfocus="this.select();" />
下面是一个带有谷歌Ajax库缓存和一些jQuery魔法的函数示例。
这将是CSS:
<style type="text/stylesheet" media="screen">
.inputblank { color:gray; } /* Class to use for blank input */
</style>
这将是JavaScript代码:
<script language="javascript"
type="text/javascript"
src="http://www.google.com/jsapi">
</script>
<script>
// Load jQuery
google.load("jquery", "1");
google.setOnLoadCallback(function() {
$("#search_form")
.submit(function() {
alert("Submitted. Value= " + $("input:first").val());
return false;
});
$("#keywords")
.focus(function() {
if ($(this).val() == 'Search') {
$(this)
.removeClass('inputblank')
.val('');
}
})
.blur(function() {
if ($(this).val() == '') {
$(this)
.addClass('inputblank')
.val('Search');
}
});
});
</script>
这就是HTML:
<form id="search_form">
<fieldset>
<legend>Search the site</legend>
<label for="keywords">Keywords:</label>
<input id="keywords" type="text" class="inputblank" value="Search"/>
</fieldset>
</form>
我希望这足以使您对GAJAXLibs和jQuery产生兴趣。
对于jQuery用户:naspinski的jQuery链接似乎坏了,但试试这个: http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
你会得到一个免费的jQuery插件教程作为奖励。:)
$('input[value="text"]').focus(function(){
if ($(this).attr('class')=='hint')
{
$(this).removeClass('hint');
$(this).val('');
}
});
$('input[value="text"]').blur(function(){
if($(this).val() == '')
{
$(this).addClass('hint');
$(this).val($(this).attr('title'));
}
});
<input type="text" value="" title="Default Watermark Text">
我发现jQuery插件jQuery水印比上面的答案中列出的要好。为什么好吗?因为它支持密码输入字段。此外,设置水印(或其他属性)的颜色就像在CSS文件中创建.watermark引用一样简单。
前段时间我在我的网站上发布了一个解决方案。要使用它,请导入一个.js文件:
<script type="text/javascript" src="/hint-textbox.js"></script>
然后用CSS类hintTextbox注释任何你想要有提示的输入:
<input type="text" name="email" value="enter email" class="hintTextbox" />
更多的信息和例子可以在这里找到。
您可以使用HTML中的placeholder属性设置占位符(浏览器支持)。可以使用CSS更改字体样式和颜色(尽管浏览器支持有限)。
input[type=search]:::-webkit-input-placeholder {/* Safari, Chrome(, Opera?) */ 颜色:灰色; 字体样式:斜体; } input[type=search]:-moz-placeholder {/* Firefox 18- */ . txt 颜色:灰色; 字体样式:斜体; } input[type=search]::-moz-占位符{/* Firefox 19+ */ 颜色:灰色; 字体样式:斜体; } input[type=search]:-ms-input-placeholder {/* IE (10+? 颜色:灰色; 字体样式:斜体; } <input placeholder="Search" type=" Search" name="q">
使用jQuery Form Notifier -它是最流行的jQuery插件之一,并且不会遭受其他jQuery建议的错误(例如,你可以自由地设置水印样式,而不用担心它会被保存到数据库中)。
jQuery水印直接在表单元素上使用单一的CSS样式(我注意到应用于水印的CSS字体大小属性也会影响文本框——这不是我想要的)。加上jQuery水印是你可以拖拽文本到字段(jQuery表单通知不允许这一点)。
另一个由其他人建议(在digitalbrush.com),会意外地提交水印值到您的表单,所以我强烈建议反对它。
另一种选择,如果你想让这个特性只适用于较新的浏览器,可以使用HTML 5的占位符属性提供的支持:
<input name="email" placeholder="Email Address">
在没有任何样式的情况下,在Chrome中看起来是这样的:
你可以在这里和HTML5的CSS占位符样式中尝试演示。
一定要检查该特性的浏览器兼容性。Firefox的支持是在3.7中添加的。Chrome浏览器没问题。Internet Explorer只在10中添加了支持。如果你的目标浏览器不支持输入占位符,你可以使用jQuery插件jQuery HTML5占位符,然后添加以下JavaScript代码来启用它。
$('input[placeholder], textarea[placeholder]').placeholder();
我喜欢“知识奇库斯”的解决方案——简单明了。只需要在页面加载就绪时添加一个模糊调用,这将设置初始状态:
$('input[value="text"]').blur();
简单的Html 'required'标签是有用的。
<form>
<input type="text" name="test" id="test" required>
<input type="submit" value="enter">
</form>
它指定在提交表单或按submit按钮之前必须填写输入字段。 这里有一个例子
现在变得很简单了。 在html中,我们可以为输入元素提供占位符属性。
e.g.
<input type="text" name="fst_name" placeholder="First Name"/>
查看更多详细信息:http://www.w3schools.com/tags/att_input_placeholder.asp
我使用了一个简单的,一行javascript解决方案,它的工作很棒。这里有一个文本框和文本区域的例子:
<textarea onfocus="if (this.value == 'Text') { this.value = ''; }" onblur="if (this.value == '') { this.value = 'Text'; }">Text</textarea>
<input type="text" value="Text" onfocus="if (this.value == 'Text') { this.value = ''; }" onblur="if (this.value == '') { this.value = 'Text'; }">
唯一的“缺点”是在对字段值做任何事情之前在$_POST或Javascript验证中进行验证。意思是,检查字段的值是不是“Text”。
可以使用占位符=""属性 下面是一个演示:
<html>
<body>
// try this out!
<input placeholder="This is my placeholder"/>
</body>
</html>