Javascript 活动无效
Javascript Events Not Working
我正在尝试 onfocus 和 onblur 事件。它不工作。当我单击电子邮件字段时,该值不会消失。这是我的代码。有什么不对的地方请告诉我。
<!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 == 'your email';
至
emailField.value = '';
...
emailField.value = 'your email';
您正在使用双等号来赋值:
emailField.value == '';
应该是
emailField.value = '';
在其他方面也是如此。
您在此处使用比较运算符 (==
) 而不是赋值运算符 (=
):
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
}
}
<form>
<input type="text" value="your email" name="email" id="email" onfocus="function(this)" />
</form>
<script>
var emailField = document.getElementById('email');
emailField.onfocus = function(c) {
if (emailField.value == 'your email')
{
emailField.value = ''; // Here was the problem use assignment operator(=) here emailField.value == '';
}
}
emailField.onblur = function(c) {
if (emailField.value == '')
{
emailField.value = 'your email'; // Here was the problem use assignment operator(=) here emailField.value == '';
}
}
</script>
我正在尝试 onfocus 和 onblur 事件。它不工作。当我单击电子邮件字段时,该值不会消失。这是我的代码。有什么不对的地方请告诉我。
<!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 == 'your email';
至
emailField.value = '';
...
emailField.value = 'your email';
您正在使用双等号来赋值:
emailField.value == '';
应该是
emailField.value = '';
在其他方面也是如此。
您在此处使用比较运算符 (==
) 而不是赋值运算符 (=
):
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
}
}
<form>
<input type="text" value="your email" name="email" id="email" onfocus="function(this)" />
</form>
<script>
var emailField = document.getElementById('email');
emailField.onfocus = function(c) {
if (emailField.value == 'your email')
{
emailField.value = ''; // Here was the problem use assignment operator(=) here emailField.value == '';
}
}
emailField.onblur = function(c) {
if (emailField.value == '')
{
emailField.value = 'your email'; // Here was the problem use assignment operator(=) here emailField.value == '';
}
}
</script>