使用 JavaScript 移除並新增新的 HTML 標記?
要移除和新增新的 HTML 標籤,請使用 hide() 和 show() 的概念。
讓我們來假設以下內容是我們的按鈕 -
<button type="submit" id="hide">Click Me To hide above content </button> <button type="submit" id="show">Click Me To show above content </button>
要在按鈕單擊時移除和新增標籤,請使用 hie() 和 show() -
$(document).ready(function(){
$("#hide").click(function(){
$("h1").hide();
});
$("#show").click(function(){
$("h1").show();
});
});示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<h1>Test JavaScript</h1>
<button type="submit" id="hide">Click Me To hide above content
</button>
<button type="submit" id="show">Click Me To show above content
</button>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("h1").hide();
});
$("#show").click(function(){
$("h1").show();
});
});
</script>
</body>
</html>要執行上述程式,請儲存檔名為 "anyName.html(index.html)",然後右鍵單擊該檔案。在 VS Code 編輯器中選擇選項“使用 Live Server 開啟”。
輸出
這將產生以下輸出 -

情況 1
當你單擊按鈕“點選我隱藏上述內容”時,你會得到以下輸出。

情況 2
當你單擊按鈕“點選我顯示上述內容”時,你會得到以下輸出。

廣告
資料結構
網路
關係型資料庫
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP