我知道其他人也问过这个问题。 我也尝试过其他人推荐的解决方案,但似乎不起作用。
这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type='text/javascript'>
$('input:text').focus(
function(){
$(this).val('');
});
</script>
</head>
<body>
<form>
<input type="text" value="Text">
</form>
</body>
</html>
最佳答案
你的问题是,当 jQuery 代码运行时,html 还不存在。因此,您只需要在页面加载时运行 jQuery,或者将该脚本发布在 <body>
之前。结束标签。
选项 1(向您的函数添加 DOM 就绪调用):
$(document).ready(function() {
$('input:text').focus(function() {
$(this).val("");
});
});
选项 2(在 body 标记结束之前添加 <script>
)
<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<form>
<input type="text" value="Text">
</form>
<script type='text/javascript'>
$('input:text').focus(
function(){
$(this).val('');
});
</script>
</body>
</html>
关于javascript - 删除输入字段的内容jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446575/