自定义getElementByClass

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>

 

posted @ 2016-02-25 08:50  点点乐淘淘  阅读(2097)  评论(0编辑  收藏  举报