问题出在哪里
我也是~
把<scirpt>放到<body>的最下面,页面加载完再进行js。这个是因为没有找到元素,console.log(oparent)为null
你是怎么解决的啊
window.onload = function () { waterfall("main","pin"); } function waterfall( parent, pin ) { var oParent = document.getElementById(parent); var oBoxs = getByClass(oParent,"pin"); // var oBoxs = oParent.getElementsByClassName("pin"); var oBoxw = oBoxs[0].offsetWidth; var cols = Math.floor(document.documentElement.clientWidth / oBoxw); oParent.style.cssText = "width:" + oBoxw*cols + "px; margin:0 auto;"; } function getByClass ( parent, cls ) { //var oParent = document.getElementById(parent); var oElements = parent.getElementsByTagName("*"); var boxArr = new Array(); for(var i = 0; i < oElements.length; i++) { if(oElements[i].className == cls){ boxArr.push(oElements[i]); } } return boxArr; }