Gau*_*ava 10 html javascript user-controls
我想在用户点击它时清除文本字段
<input name="name" type="text" id="input1" size="30" maxlength="1000" value="Enter Postcode or Area" onfocus=="this.value=''" />
ORy*_*yan 19
除非你正在做一些你想要清除onclick的特定事情,否则我建议(正如其他人已经注意到的那样)使用onfocus动作.这样,如果有人使用选项卡进行导航,也会清除默认文本.
您还可以使用onblur检查它是否为空以将其恢复:
<input type="text" value="Default text" name="yourName" onfocus="if(this.value == 'Default text') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Default text'; }">
Run Code Online (Sandbox Code Playgroud)
为此,您需要使用脚本语言,可能是javascript.这是一个例子
<input type='text' value'Some text' onclick='javascript: this.value = ""' />
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
编辑:
为了满足大卫在这里解释的是第二个例子,以防你正在寻找
<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);' />
Run Code Online (Sandbox Code Playgroud)
使用jQuery库:
<input id="clearme" value="Click me quick!" />
$('#clearme').focus(function() {
$(this).val('');
});
Run Code Online (Sandbox Code Playgroud)