获取属性需要注意的问题
1.要根据class属性拿出这个div时,document.getElementsByClassName(‘prop’)[0];
一它是className 二后面需要跟上[0];因为class属性可能不止这一个
不然就会出现 Cannot read property ‘addEventListener’ of undefined at的错误
<!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>
</head>
<style>
.content
{
width: 500px;
height: 500px;
background-color: sandybrown;
}
.box
{
width: 100px;
height: 100px;
background-color:springgreen;
}
.hezi
{
width: 50px;
height: 50px;
background-color:steelblue;
}
</style>
<body>
<div></div>
<div class="content">
<div class="box">
<div class="hezi"></div>
</div>
</div>
<script type="text/javascript">
var div =document.getElementsByTagName('div')[0];
var content = document.getElementsByClassName('content')[0];
var box = document.getElementsByClassName('box')[0];
var hezi = document.getElementsByClassName('hezi')[0];
box .addEventListener ('click',function(){
console.log('a');
},false)
content.addEventListener('click',function(){
console.log('b');
},false)
hezi.addEventListener('click',function(){
console.log('c');
},false)
</script>
</body>
</html>