设置搜索字段的默认值

编程入门 行业动态 更新时间:2024-10-25 05:11:26
设置搜索字段的默认值 - 请帮助!(Set default value for Search field - Please Help!)

我正在尝试为搜索字段设置默认值。 我们的想法是,在用户点击它之前,搜索字段的值为“搜索”,然后它应为空。 只要它是“空白”(以“搜索”作为值),它应该具有类“.blank”。

我试过这个

<input autocomplete="off" class="" id="searchq" name="searchq" onblur="if (this.value == '') { this.value='Search'; jQuery(this).addClass('blank'); };" onfocus="if (this.value == 'Search') { this.value=''; jQuery(this).removeClass('blank'); };" type="text" value="" />

它到目前为止工作,但当我加载网站时,该字段只是空的。 我必须首先在字段内单击,然后在页面上的某个位置单击以使效果起作用。

我想这与onBlur有关。 有任何想法吗?

谢谢!

I'm trying to set a default value for a search field. The idea is that the Search-Field has the value "Search" until the user clicks into it, then it should be empty. Also as long as it is "blank" (with "Search" as the value) it should have the class ".blank".

I tried this

<input autocomplete="off" class="" id="searchq" name="searchq" onblur="if (this.value == '') { this.value='Search'; jQuery(this).addClass('blank'); };" onfocus="if (this.value == 'Search') { this.value=''; jQuery(this).removeClass('blank'); };" type="text" value="" />

it works so far, but when I load the site, the field is just empty. I have to click inside the field first and then somewhere on the page to make the effect working.

I guess it has something to do with onBlur. Any ideas?

Thanks!

最满意答案

这被称为水印。 有关示例,请参见http://digitalbush.com/projects/watermark-input-plugin/

This is known as a watermark. see http://digitalbush.com/projects/watermark-input-plugin/ for an example

更多推荐

本文发布于:2023-07-31 02:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   默认值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!