js问题 代码为什么不起作用?
正确代码:<!doctypehtml><htmllang="en"><head><metacharset="UTF-8"><title>screen</title><sc...
正确代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=setInterval(func,10);
function lock(){
clearInterval(int);
document.getElementById('bu').value='Unlock';
document.getElementById('bu').onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
document.getElementById('bu').value='Lock';
document.getElementById('bu').onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html>
我只是把document.getElementById改为obj怎么不起作用啊啊啊要疯了。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=setInterval(func,10);
var obj=document.getElementById('bu')
function lock(){
clearInterval(int);
obj.value='Unlock';
obj.onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
obj.value='Lock';
obj.onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html> 展开
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=setInterval(func,10);
function lock(){
clearInterval(int);
document.getElementById('bu').value='Unlock';
document.getElementById('bu').onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
document.getElementById('bu').value='Lock';
document.getElementById('bu').onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html>
我只是把document.getElementById改为obj怎么不起作用啊啊啊要疯了。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=setInterval(func,10);
var obj=document.getElementById('bu')
function lock(){
clearInterval(int);
obj.value='Unlock';
obj.onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
obj.value='Lock';
obj.onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html> 展开
1个回答
展开全部
我觉得应该是页面没有加载完成,你就去document.getElementById了,所以,你把获取值放到window.onload = function(){}中。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=null;
var obj=null;
window.onload = function(){
int=setInterval(func,10);
obj=document.getElementById('bu')
}
function lock(){
clearInterval(int);
obj.value='Unlock';
obj.onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
obj.value='Lock';
obj.onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>screen</title>
<script type="text/javascript">
function func(){
document.getElementById('txt').value='宽:'+window.innerWidth+'高:'+window.innerHeight;
}
var int=null;
var obj=null;
window.onload = function(){
int=setInterval(func,10);
obj=document.getElementById('bu')
}
function lock(){
clearInterval(int);
obj.value='Unlock';
obj.onclick=function(){
unlock()
}
}
function unlock(){
var int=setInterval(func,10);
obj.value='Lock';
obj.onclick=function(){
lock()
}
}
</script>
</head>
<body>
<input type='text' id='txt'/>
<input type='button' onclick='lock()' value='Lock' id='bu'/>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询