window.onload = function(){ waterfall('main','box'); } function waterfall(parent,box){ //将main下所有class=box的元素取出来 var oParent = document.getElementById(parent); var oBox = getbyclass(oParent,box); console.log(oBox.length); } function getbyclass(parent,clsname){ var boxarr = [];//用来存储所有class = box 的元素 oElements = parent.getElementsByTagName("*"); for (var i = 0; i < oElements.length; i++) { if (oElements[i].className == clsname) { boxarr.push(oElements[i]); } } return boxarr; }
报错:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null at getbyclass (index.js:18) at waterfall (index.js:10) at window.onload (index.js:2)
第十一行 box 是字符串
。。。。知道哪里错了