如何使用 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>
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP