javascript: setting array index value won't work?
I have an array of divs called "array" that I gathered from the DOM using getElementsByTagName. One of the divs is null (at "nullindex"), and I want to switch its location with the location of one of the other divs (at "index"). I tried:
array[nullindex] = array[index];
array[index] = null;
alert(array[index].innerHTML + "," + array[nullindex].innerHTML);
nullindex = index;
This should switch the locations of the divs and set "nullindex" to its new location, right? But the alert call produces the same innerHTML for bot开发者_如何学Pythonh, meaning the div was copied, not switched, and what's supposed to be null is not. So the next time I'm iterating through to find the div I want, it's still at its original position. HELP!!
The problem is the 'array' returned from getElementsByTagName is not an actual array. Before manipulating the elements, run this code:
array = Array.prototype.slice.apply(array);
精彩评论