課程
/前端開發
/JavaScript
/瀑布流布局
為什么我跟老師打的一模一樣,chrom報錯,說getElementsByTagName() unll
2017-04-24
源自:瀑布流布局 2-3
正在回答
我有個方法,試試吧var oParent=document.getElementById('parent');當中的引號去掉,他們就能找到main元素,然后就正常了
我的也是這樣
window.onload=function(){
waterfall('main','box');
}
function waterfall(parent,box){
//取出class為box的元素
var oParent=document.getElementById('parent');
var oBoxs=getByClass(oParent,box);
//計算整個頁面顯示的列數(頁面的款的除以每個盒子的寬度)
var oBoxW=oBoxs[0].offsetWidth;
console.log(oBoxw);
function getByClass(parent,clsName){
var boxArr=new Array();
var oElements=parent.getElementsByTagName("*");
for(var i=0;i<oElements.length;i++){
if (oElements[i].className==clsName){
boxArr.push(oElements[i]);
return boxArr;
顯示為null
貼代碼看下
舉報
瀑布流布局是網站比較流行的一種布局方式,教你實現三大方式
1 回答getElementsByTagName報錯
5 回答Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined
4 回答老是提示 Cannot read property 'getElementsByTagName' of null
1 回答mian.js:18 Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-05-10
我有個方法,試試吧var oParent=document.getElementById('parent');當中的引號去掉,他們就能找到main元素,然后就正常了
2017-04-28
我的也是這樣
window.onload=function(){
waterfall('main','box');
}
function waterfall(parent,box){
//取出class為box的元素
var oParent=document.getElementById('parent');
var oBoxs=getByClass(oParent,box);
//計算整個頁面顯示的列數(頁面的款的除以每個盒子的寬度)
var oBoxW=oBoxs[0].offsetWidth;
console.log(oBoxw);
}
function getByClass(parent,clsName){
var boxArr=new Array();
var oElements=parent.getElementsByTagName("*");
for(var i=0;i<oElements.length;i++){
if (oElements[i].className==clsName){
boxArr.push(oElements[i]);
}
}
return boxArr;
}
顯示為null
2017-04-25
貼代碼看下