<script></script>标签放的位置:
一、放在<head></head>中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 50px;
height: 50px;
background-color: aquamarine;
}
</style>
<script type="text/javascript">
window.onload =function(){
var value=document.getElementById("item");
value.onclick=function(){
value.style.background = "#ffefa1"
}
}
</script>
</head>
<body>
<div id="item"></div>
</body>
</html>
注意:放在<head></head>中,需要在<script></script>中用window.onload()
二、放在<body></body>中
不需要加window.onload()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 50px;
height: 50px;
background-color: aquamarine;
}
</style>
</head>
<body>
<div id="item"></div>
<script type="text/javascript">
var value=document.getElementById("item");
value.onclick=function(){
value.style.background = "#ffefa1"
}
</script>
</body>
</html>