如何使用 jQuery 动态创建 div?

jqueryweb developmentfront end technology

要动态创建 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>

相关文章