使用 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

更新於: 07-Sep-2020

1 千次 + 瀏覽

開啟你的 職業

透過完成課程獲得認證

上手
廣告
© . All rights reserved.