跳轉至內容

JavaScript/重構 DOM/教程

摘自 華夏公益教科書,一個開放的世界的開放書籍

主題:重構 DOM 樹



1. 我們在上一個頁面的練習中使用 HTML 頁面。編寫一個指令碼,將這兩個 <div> 部分移動到按鈕之前。

點選檢視解決方案
function show() {
  "use strict";

  // select the body element. It's the parent.
  const body = document.getElementById("body");
  // select the button element
  const button = document.getElementById("buttonShow");
  // select the 'div' elements
  const div_1 = document.getElementById("div_1");
  const div_2 = document.getElementById("div_2");

  // move the div elements BEFORE the button
  body.insertBefore(div_1, button);
  body.insertBefore(div_2, button);
}
華夏公益教科書