HTMLhref 屬性


<base> 元素的 href 屬性為所有相對 URL 設定基本 URL。 例如,基本 URL 為https://example.com/tutorials,對於所有的相對 URL,比如 /html, /java, /jquery 等,最後結果為-

https://example.com/tutorials/html
https://example.com/tutorials/java
https://example.com/tutorials/jquery

以下是語法 −

<base href="absolute_URL">

在上面,絕對的 _URL 是基本 URL 的絕對 url。 現在,讓我們看一個示例來實現 <base> 元素的 href 屬性 −

示例

 即時演示

<!DOCTYPE html>
<html>
<head>
<base href="https://www.example.com/tutorials/">
</head>
<body>
   <h2>Tutorials List</h2>
   <p><a href="java.html">Java Tutorial</a></p><p>(This will act as https://www.example.com/tutorials/java.html)</p>
   <p><a href="jquery.html">jQuery Tutorial</a></p><p>(This will act as https://www.example.com/tutorials/jquery.html)</p>
   <p><a href="blockchain.html">Blockchain Tutorial</a></p><p>(This will act as https://www.example.com/tutorials/blockchain.html)</p>
   <p><a href="python.html">Python Tutorial</a></p><p>(This will act as https://www.example.com/tutorials/python.html)</p>
</body>
</html>

輸出

在上述示例中,我們把基本 URL 設定為 −

<base href="https://www.example.com/tutorials/">

在我們引用java.html後,最終它起的作用類似於 −

https://www.example.com/tutorials/java.html

更新於: 29-Jun-2020

156 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.