开发者

clear text onclick - textfield

What is t开发者_开发技巧he best method to clear text in a text field, when the user clicks on it?

I.e., if the text search field says "search" and when they click it, it clears the value.


You could do like:

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


<input name="foo" placeholder="Search" /> The placeholder tag is a new HTML5 tag that does exactly what you want to do here.


Normal HTML:

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

jQuery:

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


In JavaScript you can simple set the value of the contorol to an empty string in the OnFocus event handler.


If jquery's an option, I'd consider the watermark plugin

It achieves what you're after, but with a bit more style

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜