html DOM Link href 屬性
HTML DOM Link href 屬性設定/返回連結文件的路徑/url。−
語法
以下是語法 −
- 返回href屬性值
linkObject.href
- 將href設定為字串
linkObject.href = string
布林值
這裡,“字串”可以是以下內容 −
布林值 | 詳細資訊 |
---|---|
路徑 | 它定義文件的絕對/相對路徑。 |
url | 它定義要連結的文件的 url 地址。 |
示例
讓我們看看Link href屬性的一個示例 −
<!DOCTYPE html> <html> <head> <title>Link href</title> <link id="extStyle" rel="stylesheet" type="text/css" href="style.css"> </head> <body> <form> <fieldset> <legend>Link-href</legend> <label for="WeekSelect">Sales Target Week: <input type="week" id="WeekSelect" value="2020-W13" disabled> </label> <input type="button" onclick="enableWeekInput()" value="Change Sales Target Week"> <input type="button" onclick="changeStyle()" value="Change Style"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var inputWeek = document.getElementById("WeekSelect"); var extStyle = document.getElementById("extStyle"); divDisplay.textContent = 'Week Input disabled: '+inputWeek.disabled; function enableWeekInput() { inputWeek.disabled = false; divDisplay.textContent = 'Week Input disabled: '+inputWeek.disabled; } function changeStyle(){ extStyle.href = 'anotherStyle.css'; } </script> </body> </html>
在上面的示例中‘style.css’包含 −
form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; }
在上面的示例中‘anotherStyle.css’包含 −
form { width:70%; margin: 0 auto; text-align: center; background-color: rgba(0,0,0,0.7); color: white; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; }
輸出
將產生以下輸出 −
在點選‘Change Styling’按鈕前 −
點選‘Change Styling’按鈕後 −
廣告