0

単純なコードに問題がある

    var x = document.getElementsByTagName("th");
    var plop = Array.prototype.slice(x);
    console.log(x);
    console.log(x.length);
    console.log(plop);

出力

[item: function, namedItem: function]
0: th.fc-day-header.fc-sun.fc-widget-header.fc-first
1: th.fc-day-header.fc-mon.fc-widget-header
2: th.fc-day-header.fc-tue.fc-widget-header
3: th.fc-day-header.fc-wed.fc-widget-header
4: th.fc-day-header.fc-thu.fc-widget-header
5: th.fc-day-header.fc-fri.fc-widget-header
6: th.fc-day-header.fc-sat.fc-widget-header.fc-last
length: 7__proto__: HTMLCollection
    controller.js:309 0
    controller.js:310 []

ここで length 7 が表示され、 x.length が 0 になるのはなぜですか?

前もって感謝します

更新:コードを設定しました

$scope.$on('$viewContentLoaded', function(){
}

そして、それは今動作します。私はまだ onclick メソッドを設定することはできませんが、進行しています ^^

4

2 に答える 2

0

.call()から配列のコピーを作成しようとしているときに、 がありませんHTMLCollection。これの代わりに:

var x = document.getElementsByTagName("th");
var plop = Array.prototype.slice(x);

あなたはこれを行うことができます:

var x = document.getElementsByTagName("th");
var plop = Array.prototype.slice.call(x);
console.log(plop);

注: 反復するためだけに配列のコピーを作成する必要はありません。HTMLCollection を直接反復できます。

var items = document.getElementsByTagName("th");
for (var i = 0; i < items.length; i++) {
    console.log(items[i]);
}
于 2015-02-02T20:15:08.353 に答える