一段小代码,求为什么js不能运行
<!doctypehtml><html><head><metacharset="utf-8"><script>vardiv1=document.getElementByI...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div2.onclick = function(){
div1.style = "left:30px";
};
</script>
<title>无标题文档</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#div1 {
width:200px;
height:80px;
background:#039;
position:absolute;
top:10px;
}
#div2 {
width:100px;
height:100px;
background:#0FF;
position:absolute;
}
</style>
</head>
<body>
<div id="div1">
div1
</div>
<div id="div2">
div2
</div>
</body>
</html> 展开
<html>
<head>
<meta charset="utf-8">
<script>
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div2.onclick = function(){
div1.style = "left:30px";
};
</script>
<title>无标题文档</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#div1 {
width:200px;
height:80px;
background:#039;
position:absolute;
top:10px;
}
#div2 {
width:100px;
height:100px;
background:#0FF;
position:absolute;
}
</style>
</head>
<body>
<div id="div1">
div1
</div>
<div id="div2">
div2
</div>
</body>
</html> 展开
展开全部
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
#div1 {
width:200px;
height:80px;
background:#039;
position:absolute;
top:10px;
}
#div2 {
width:100px;
height:100px;
background:#0FF;
position:absolute;
}
</style>
</head>
<body>
<div id="div1">
div1
</div>
<div id="div2">
div2
</div>
</body>
<script>
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div2.onclick = function(){
div1.style = "left:30px";
};
</script>
</html>
你以后所有的js代码也最好这样写、放在下面、
从上往下执行、如果放在上面、你js执行的时候div的dom节点都没有生成、你怎么会获取得到。
展开全部
把script 放到</body> 之前
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//js代码
<script>
function dianji() {
var div1 = document.getElementById("div1");
div1.style = "left:30px";
}
</script>
<div id="div2" onclick="dianji()">
<script>
function dianji() {
var div1 = document.getElementById("div1");
div1.style = "left:30px";
}
</script>
<div id="div2" onclick="dianji()">
更多追问追答
追问
onmouseover能不能实现left:20px?
追答
能 就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询