0
我嘗試編寫一個Safari擴展,它使用表格可視化網站上的一些數據。所以,要計算我做了一些JavaScript的數據:Safari擴展中的DOM修改
var table;
table = document.body.getElementsByClassName('summary');
console.log(table);
var rows = table.getElementsByTagName('tr');
console.log(rows);
第二行工作正常,但對於第五行我得到這個錯誤:
TypeError: 'undefined' is not a function (evaluating 'table.getElementsByTagName('tr')')
這是爲什麼? Whith this code:
document.body.getElementsByClassName('summary').getElementsByTagName('tr');
這是一樣的。
我的錯誤是什麼?
就是這樣!非常感謝你!我意識到Iam在JavaScript中還不夠好。 :-) – Juuro 2012-03-19 14:45:43
微調:getElementsByClassName的結果不是一個真正的數組,而是一個[NodeList](https://developer.mozilla.org/en/DOM/NodeList),它是一個類似數組的對象。不要嘗試在NodeList上使用像pop和forEach這樣的數組方法。 – canisbos 2012-03-19 20:20:24