HTML DOM Link href 属性

htmlweb developmentfront end technology更新于 2025/7/14 17:16:17

HTML DOM Link href 属性设置/返回链接文档的路径/URL。−

语法

以下是语法 −

  • 返回 href 属性值
linkObject.href
  • 将 href 设置为字符串
linkObject.href = string

布尔值

这里,"string"可以是以下 −

booleanValue
详细信息
path
定义文档的绝对/相对路径。
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;
}

In the above example ‘anotherStyle.css’ contains −

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

输出

这将产生以下输出 −

点击"更改样式"按钮之前 −

点击"更改样式"按钮之后 −


相关文章


有用资源