DOM已经实现了getElementByClass,这个功能内部是怎么实现的呢
js代码及如何使用:
function getElementByClass(className,parentNode){ parentNode = parentNode || document; var childrens = parentNode.getElementsByTagName(‘*‘); var nodeArr = []; for(var i=0;i<childrens.length;i++){ if(childrens[i].className == className){ nodeArr.push(childrens[i]); } } return nodeArr; } var parentNode = document.getElementById(‘target‘); var classNode = getElementByClass("pullright"); console.log(classNode);
html代码:
<div class="container" id="target"> <div class="pullleft"> <span>这里是左浮动</span> </div> <div class="pullright"> <span class="pullleft">这里是右浮动内容</span> </div> <p class="pullleft">这里是需要浮动的文本</p> </div>
时间: 2024-10-11 11:32:55