老师有问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>指定匹配位置</title>
</head>
<body> <p class="odd odd1">1</p>
<p class="even">2</p>
<p class="odd">3</p>
<p class="even">4</p> <script type="text/javascript">
var oddP = getByClassName('odd');
var evenP = getByClassName('even');
for(var i = 0; i < oddP.length; i++){
oddP[i].style.backgroundColor = 'red';
}
for(var i = 0; i < evenP.length; i++){
evenP[i].style.backgroundColor = 'yellow';
}
function getByClassName(className,parentNode){
if(!document.getElementsByClassName){
return document.getElementsByClassName(className);
}else{console.log(1);
parentNode = parentNode || document;
var nodeList = [];
// 获取所有元素
var allNodes = parentNode.getElementsByTagName('*');
for (var i = 0; i < allNodes.length; i++) {
if (className.indexOf(allNodes[i].className) != -1) {
nodeList.push(allNodes[i]);
}
}
return nodeList;
}
}
</script>
</body>
</html>
className.indexOf(allNodes[i].className) != -1
我这样改为什么不可以呢
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星