利用封裝好的each來繼續操作樣式4
阿新 • • 發佈:2018-12-03
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div,span{
width: 100px;
height: 30px;
border: 1px solid red;
padding : 5px 5px;
}
</style>
</head>
<body class="box">
<div ></div>
<div></div>
<span></span>
<span></span>
</body>
</html>
<script type="text/javascript">
var getdom = function(tag){
return document.getElementsByTagName(tag);
}
var each = function(arr,fn){
for(var i=0;i<arr.length;i++){
if(fn.call(arr[i],i,arr[i]) === false){
break;
}
}
}
var list1 = getdom('div');
var list2 = getdom('span');
each(list1,function(){
this.style.background = 'red';
})
each(list2,function(){
this.style.background = 'green';
})
</script>