gpt4 book ai didi

JavaScript 事件 onfocus 和 onblur

转载 作者:行者123 更新时间:2023-11-30 10:03:00 26 4
gpt4 key购买 nike

我正在尝试 onfocusonblur 事件。它不工作。当我单击电子邮件字段时,该值不会消失。这是我的代码。如果有什么问题请告诉我:

    <!DOCTYPE html>
<html>
<head>
<title>More Events</title>
</head>
<body>
<form>
<input type="text" value="your email" name="email" id="email" />
</form>

<script>
var emailField = document.getElementById('email');
emailField.onfocus = function(c) {
if (emailField.value == 'your email')
{
emailField.value == '';
}
}
emailField.onblur = function(c) {
if (emailField.value == '')
{
emailField.value == 'your email';
}

}
</script>

</body>
</html>

最佳答案

您在这里使用比较运算符(==) 而不是赋值运算符(=):

emailField.value == '';

改成

emailField.value = '';

在代码中:

var emailField = document.getElementById('email');
emailField.onfocus = function(c) {
if (emailField.value == 'your email')
{
emailField.value = '';
}
}
emailField.onblur = function(c) {
if (emailField.value == '')
{
emailField.value = 'your email';//Also here
}

}

关于JavaScript 事件 onfocus 和 onblur,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30613640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com