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’按钮后 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP