Link hrefプロパティ
Link オブジェクト
定義と使い方
href プロパティは読み書き可能な文字列で、現在表示されているドキュメントの完全な URL を設定または返します。
構文
href プロパティを設定:
linkObject.href="URL.html"
href プロパティを返す:
linkObject.href
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
すべての主要ブラウザが href プロパティをサポートしています
例
例
ボタンをクリックするとスタイルシートを切り替えます:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<link id="style1" rel="stylesheet" type="text/css" href="style1.css.html">
<script>
function displayResult(){
document.getElementById("style1").href="style2.css.html";
}
</script>
</head>
<body>
<h1>私のウェブサイトのホームページ</h1>
<button type="button" onclick="displayResult()">スタイルシートを切り替える</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<link id="style1" rel="stylesheet" type="text/css" href="style1.css.html">
<script>
function displayResult(){
document.getElementById("style1").href="style2.css.html";
}
</script>
</head>
<body>
<h1>私のウェブサイトのホームページ</h1>
<button type="button" onclick="displayResult()">スタイルシートを切り替える</button>
</body>
</html>
試してみる »
Link オブジェクト その他の拡張