javascript ノードを移動させる
- 作成日 2021.01.16
- 更新日 2022.08.03
- javascript
- javascript

javascriptで、appendChildを使用して、ノードを移動させるサンプルコードを掲載してます。ブラウザはchromeを使用しています。
環境
- OS windows10 pro 64bit
- Apache 2.4.43
- ブラウザ chrome 103.0.5060.134
appendChild使い方
親要素に対して「appendChild」を使用すると、ノードを移動させることが可能です。
親要素.appendChild(追加したい要素);
指定したノードを、div要素の最後尾に移動させてみます。
<div id="main">
<p id="one">one</p>
<p id="two">two</p>
</div>
<script>
'use strict';
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("main");
elm2.appendChild(elm1);
</script>
実行結果をみると、指定したノードが最後尾に移動されて表示されていることが確認できます。

子要素を含んでる場合は、子要素も一緒に移動します。
<div id="main">
<p id="one">one</p>
<p id="two">two<b>child</b></p>
</div>
<script>
'use strict';
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("main");
elm2.appendChild(elm1);
</script>
実行結果

insertBeforeを使用
「insertBefore」の第2引数を「null」に指定すると、同様に最後尾にノードが追加されるので、同様の結果を得られます。
<div id="main">
<p id="one">one</p>
<p id="two">two</p>
</div>
<script>
'use strict';
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("main");
elm2.insertBefore(elm1,null);
</script>
「parentNode」で親要素を取得して実行しても同じ結果となります。
const elm1 = document.getElementById("one");
const elm2 = elm1.parentNode;
elm2.insertBefore(elm1,null);
また、以下のコードを、
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("main");
elm2.appendChild(elm1);
document.getElementByIdの省略して、簡潔に記述することもできます。
main.appendChild(one);
存在しない要素を指定
存在しない要素を指定した場合は、エラーが発生します。
<div id="main">
<p id="one">one</p>
<p id="two">two</p>
</div>
<script>
'use strict';
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("xxx");
elm2.appendChild(elm1);
// Uncaught TypeError: Cannot read properties of null (reading 'appendChild')
</script>
なので、存在チェックをしてから使用します。
const elm1 = document.getElementById("one");
const elm2 = document.getElementById("xxx");
if(elm1 !== null && elm2 !== null){
elm2.appendChild(elm1);
}
document.getElementByIdを省略している場合は、「typeof」を使用します。
if(typeof one !== 'undefined' && typeof xxx !== 'undefined' ){
xxx.appendChild(one);
}
サンプルコード
以下は、
「ノードを移動」ボタンをクリックして、ノードを移動させるだけの
サンプルコードとなります。
※cssには「tailwind」を使用して、アロー関数で関数は定義してます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>mebeeサンプル</title>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<script>
window.onload = () => {
add.onclick = () => { main.appendChild(one); };
}
</script>
<body>
<div class="container mx-auto my-56 w-64 px-4">
<div id="sample" class="flex flex-col justify-center">
<button id="add"
class="bg-gradient-to-r from-green-400 to-blue-500 hover:from-pink-500 hover:to-yellow-500 text-white py-2 px-4 rounded-full mb-3 mt-4">
ノードを移動
</button>
<div id="main" class="space-y-4">
<span id="one" class="block rounded-md text-white font-extrabold text-center bg-purple-500 p-6">1</span>
<span id="two" class="block rounded-md text-white font-extrabold text-center bg-purple-500 p-6">2</span>
<span id="three" class="block rounded-md text-white font-extrabold text-center bg-purple-500 p-6">3</span>
</div>
</div>
</div>
</body>
</html>
ノードが移動されていることが確認できます。

-
前の記事
rails6 APIを実装する 2021.01.16
-
次の記事
go言語 文字列に使用されている文字数を取得する 2021.01.16
コメントを書く