我正在尝试为搜索字段设置默认值。 我们的想法是,在用户点击它之前,搜索字段的值为“搜索”,然后它应为空。 只要它是“空白”(以“搜索”作为值),它应该具有类“.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
更多推荐
发布评论