我正在做的事情可能真的很愚蠢,但我搜索了很多,并且在我的一生中我没有得到什么问题(网页设计/编程的新功能),而且我知道当文本框失去焦点时它应该触发但事实并非如此:
<html>
<head>
<title>ask9</title>
<script>
function changeColor(){
var x=document.getElementById("box").value;
if(isNaN(x)==false)
{
body.style.backgroundColor="yellow";
}
else if(isNaN(x)==true)
{
body.style.backgroundColor="green";
}
else
{
body.style.backgroundColor="red";
}
}
</script>
</head>
<body>
<input type="text" id="box" onchange="changeColor()">
</body>
</html>
最佳答案
body
未定义 - 您可能指的是 document.body
或 document.getElementsByTagName('body')[0]
- 请参阅这个jsFiddle:http://jsfiddle.net/LrrpM/
<html>
<head>
<title>ask9</title>
<script>
function changeColor() {
var x=document.getElementById("box").value;
if(isNaN(x)==false)
{
document.body.style.backgroundColor="yellow";
}
else if(isNaN(x)==true)
{
document.body.style.backgroundColor="green";
}
/* // will never occur, isNaN() always returns a boolean
else
{
document.body.style.backgroundColor="red";
}*/
}
</script>
</head>
<body>
<input type="text" id="box" onchange="changeColor()">
</body>
</html>
关于javascript - Onchange 不火?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345193/