本文介绍了单击后如何清除输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,单击后如何清除输入文本?默认情况下,该值保留在输入字段中.

Using jQuery, how can I clear the input text after I made a click? By default, the value remains in the input field.

例如,我有一个输入文本,值是TEXT.单击时,我希望输入字段为空.

For example, I have an input text and the value is TEXT. When I perform a click, I want the input field to become empty.

推荐答案

要删除默认文本,请单击以下元素:

To remove the default text, on clicking the element:

$('input:text').click(
    function(){
        $(this).val('');
    });

不过,我建议改用focus():

$('input:text').focus(
    function(){
        $(this).val('');
    });

哪个也会响应键盘事件(例如,Tab键).另外,您可以在元素中使用placeholder属性:

Which responds to keyboard events too (the tab key, for example). Also, you could use the placeholder attribute in the element:

<input type="text" placeholder="default text" />

这将清除元素的焦点,并在元素保持空白/用户未输入任何内容时重新出现.

Which will clear on focus of the element, and reappear if the element remains empty/user doesn't input anything.

这篇关于单击后如何清除输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:41
查看更多