问题背景 :
想制作一个网页,div中的背景颜色根据标签中的值变化。
使用innerHTML取值只能取一个值,怎么样将所有值取出加以判断?
问题背景 :
想制作一个网页,div中的背景颜色根据标签中的值变化。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
.type{background: red;width: 20px;}
</style>
</head>
<body>
<div class="type">1</div>
<div class="type">2</div>
<div class="type">3</div>
<div class="type">4</div>
<div class="type">5</div>
<div class="type">6</div>
</body>
</html>
<script>
var type = document.getElementsByClassName('type')
for(var i=0; i<type.length;i++){
if(type[i].innerHTML==='2'){
type[i].style.backgroundColor='blue'
}
}
</script>