JavaScript で多数の div を作成していますが、ある時点ですべての div を削除したいと考えています。
私のコードは次のようなものです:
function CreateDiv(width, height, row, col){
var thisTile = document.createElement("div");
thisTile.style.position = "absolute";
thisTile.style.width = width + "px";
thisTile.style.height = height + "px";
thisTile.style.top = row*TileH + topMargin + "px";
thisTile.style.left = col*TileW + leftMargin +"px";
thisTile.style.backgroundImage = "url(" + imagePath + ")";
thisTile.style.backgroundSize = imageWidth + "px " + imageHeight +"px";
thisTile.style.backgroundRepeat = "noRepeat";
thisTile.style.backgroundPosition = "-" + col*TileW + "px -" + row*TileH + "px";
thisTile.onclick = TileClicked;
thisTile.name = "tiles";
document.body.appendChild(thisTile);
return thisTile;
}
...
var tmp = document.getElementsByName("tiles");
alert("tmp length: " + tmp.length);
for (var i = 0; i < tmp.length; i++)
document.body.removeChild(tmp[i]);
しかし、tmp が空の配列になるたびに、必要な div を実際に削除することはできません。
変えてみた
tile.name = "tiles"
に
tile.nodeName = "tiles"
また
tile.className = "tiles"
しかし、どれも機能しませんでした。要素のどの名前属性またはプロパティが正確に getElementsByName のものなのだろうか?