Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính href của Link trong HTML DOM

Thuộc tính href của Link trong HTML DOM thiết lập/trả về đường dẫn/URL của tài liệu được liên kết.

Cú pháp

Trả về giá trị thuộc tính href:

linkObject.href

Thiết lập href thành chuỗi:

linkObject.href = string

Các giá trị chuỗi

string có thể là:

LoạiMô tả
pathĐịnh nghĩa đường dẫn tuyệt đối/đối tượng đến tài liệu.
urlĐịnh nghĩa địa chỉ URL của tài liệu được liên kết.

Ví dụ

Ví dụ minh họa thuộc tính 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">Tuần mục tiêu doanh số: <input type="week" id="WeekSelect" value="2020-W13" disabled> </label> <input type="button" onclick="enableWeekInput()" value="Thay đổi tuần mục tiêu doanh số"> <input type="button" onclick="changeStyle()" value="Thay đổi giao diện"> <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>

Trong ví dụ trên 'style.css' chứa:

form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; }

Trong ví dụ trên 'anotherStyle.css' chứa:

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; }

Kết quả

Trước khi nhấn nút 'Thay đổi giao diện':

Thuộc tính href của Link trong HTML DOM

Sau khi nhấn nút 'Thay đổi giao diện':

Thuộc tính href của Link trong HTML DOM