I have an issue with a simple code
var x = document.getElementsByTagName("th");
var plop = Array.prototype.slice(x);
console.log(x);
console.log(x.length);
console.log(plop);
output
[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 []
Why does I see lenght 7 here and the x.length gives 0 ?
Thanks in advance
update : I set the code in
$scope.$on('$viewContentLoaded', function(){
}
And it works now. I'm not yet able to set an onclick method but it progress ^^
$scope.$on('$viewContentLoaded', function(){
var x = document.getElementsByTagName("th");
var arr = Array.prototype.slice.call(x);
console.log(arr);
for (var i = 0; i < arr.length; i++) {
// console.log("iterate");
console.log(arr[i]);
arr[i].onclick = function(){
console.log("lol");
};
}
};
You are missing the .call()
when you're trying to make an array copy from the HTMLCollection
. Instead of this:
var x = document.getElementsByTagName("th");
var plop = Array.prototype.slice(x);
You can do this:
var x = document.getElementsByTagName("th");
var plop = Array.prototype.slice.call(x);
console.log(plop);
Note: you don't need to make an array copy just to iterate over it. You can iterate an HTMLCollection directly:
var items = document.getElementsByTagName("th");
for (var i = 0; i < items.length; i++) {
console.log(items[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.