document.querySelectorAll restituisce una NodeList, non un array.
quindi non v'è alcun metodo su Splice NodeList Per default.
Tuttavia è possibile prototipare un metodo simile per un elenco di nodi.
Ecco una JSFiddle di lavoro, esso rimuove elementi come giunzione direttamente dal DOM, è possibile modificarlo tuttavia si desidera.
var myArray = [];
myArray = document.querySelectorAll('.selected');
//This is a primitive analogue of splice without adding new elements, it will not remove element from NodeList, however will remove it directly from dome, then it will return the resulting array (As Array), because NodeList is unmodifiable;
NodeList.prototype.splice = function(pos, numToRemove){
var initRemCount = remCount = numToRemove ? numToRemove : 1;
var removed = [];
for(var i = 0; i < this.length; i++){
if(!remCount)
break;
var elm = this[i];
if(i >= pos){
//elm.parentElement.removeChild(elm); //I commented this out, 'cause you say you dont want to delete members from DOM, uncomment this to do so
remCount--;
}
}
return [].slice.call(this, pos, pos + initRemCount);
}
var resultArray = myArray.splice(2, 2);
//This is the Araay already not a NodeList
console.log(resultArray);
fonte
2014-12-24 13:22:00
puoi mostrarci il codice HTML, con la classe selezionata? –
myArray è un Nodelist non un array normale – SNAG
Spero che tu non stia immaginando che rimuovere gli elementi dall'elenco dei nodi li rimuoverà dal documento. Perché vuoi rimuovere elementi dall'array? –