如何在文本字段的焦点上清除文本字段

2022-01-22 00:00:00 user-controls javascript html

我想在用户点击时清除文本字段

I want to clear the text field when the user clicks on that

推荐答案

为此,您需要使用脚本语言,可能是 javascript.这里是一个例子

To do this you will need to use a scripting language, probably javascript. Here an example

<input type='text' value'Some text' onclick='javascript: this.value = ""' />

希望这会有所帮助.

为了满足大卫在这里解释的第二个例子,以防你正在寻找什么

To meet what David is explain here is a second example in case that is what you are looking for

<script type='javascript'>
    var clear = true;
    function clear(obj)
    {
        if(clear)
        {
            obj.value = '';
            clear = false;
        }
    }
</script>

<input type='text' value'Some text' onfocus='clear(this);' />

相关文章