简体   繁体   English

明文onclick - textfield

[英]clear text onclick - textfield

What is the best method to clear text in a text field, when the user clicks on it? 当用户点击文本字段时,清除文本字段中文本的最佳方法是什么?

Ie, if the text search field says "search" and when they click it, it clears the value. 即,如果文本搜索字段显示“搜索”并且当它们单击它时,它将清除该值。

你可以这样做:

<input type="text" onfocus="if(this.value == 'search') {this.value=''}" onblur="if(this.value == ''){this.value ='search'}">

<input name="foo" placeholder="Search" />占位符标记是一个新的HTML5标记,它完全符合您的要求。

Normal HTML: 普通HTML:

 <script type="text/javascript">
    function clearThis(target){
        target.value= "";
    }
    </script>
    <input type="text" value="Search" onfocus="clearThis(this)" />

jQuery: jQuery的:

<script type="text/javascript">
    function clearThis(target){
        $(target).val = "";
    }
</script>
<input type="text" value="Search" onfocus="clearThis(this)" />

在JavaScript中,您可以简单地将contorol的值设置为OnFocus事件处理程序中的空字符串。

If jquery's an option, I'd consider the watermark plugin 如果jquery是一个选项,我会考虑水印插件

It achieves what you're after, but with a bit more style 它实现了你所追求的,但更具风格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM