如何使用 jQuery 動態建立 div?


要動態建立 div,請使用 html() 方法。你可以嘗試執行以下程式碼,學習如何在按鈕單擊時動態建立 div −

示例

線上演示

<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $("button").click(function(){
        $("body").html("<div>This is a new div.</div>");
      });
    });
</script>
</head>
<body>

  <button>Create a new div</button>

<p>This is demo text.</p>
</body>
</html>

更新時間:20-Jun-2020

1K+ 瀏覽量

開啟你的職業生涯

完成課程以獲得認證

開始
廣告