使用 JavaScript 為文件建立帶自定義 URL 的 HTML 文件
你可以使用 createHTMLDocument() 的概念。以下為程式碼 −
示例
<!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>
<script>
const htmlDocument = document.implementation.createHTMLDocument();
const customURL = htmlDocument.createElement( 'base' );
customURL.href = "https://tutorialspoint.tw/java/index.htm";
htmlDocument.head.append( customURL );
console.log("Base URL="+customURL.href);
const modifiedURL = htmlDocument.createElement("a");
modifiedURL.href = "../java/java_questions_answers.html";
htmlDocument.body.append( modifiedURL );
console.log("After Modifying URL="+ modifiedURL.href );
</script>
</body>
</html>要執行上述程式,請儲存檔名“anyName.html(index.html)”並右鍵單擊該檔案。在 VS Code 編輯器中,選擇“使用 Live Server 開啟”選項。
輸出
這將在控制檯上生成以下輸出 −
Base URL=https://tutorialspoint.tw/java/index.htm After Modifying URL=https://tutorialspoint.tw/java/java_questions_answers.html
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP