Home » ★JavaScriptリファレンス

★JavaScriptリファレンス

document.イメージ名.src …… 画像ファイルのURLを参照する
Internet Explorer
Google Chrome
Safari
Firefox
Opera
広告



イメージオブジェクトのsrcプロパティは、画像ファイルのURLを参照・設定します。

■構文・引数・戻り値

構文
イメージオブジェクト名.src
戻り値
画像ファイルのURL

■使用例1 画像ファイルのURLを参照

HTML + JavaScriptソース

<p>
<img name="samplekaeru" src="https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/kaeru.gif">
</p>

<script>
<!--
var imgurl = document.samplekaeru.src;
document.write('カエル画像のURLは');
document.write(imgurl);
document.write('です。');
// -->
</script>

↓↓↓

ブラウザ上の表示

■使用例2 画像ファイルのURLを設定

HTML + JavaScriptソース

<script>
<!--
function changeimg1() {
	document.samplepanda.src = "../images/panda1.gif";
}

function changeimg2() {
	document.samplepanda.src = "../images/panda2.gif";
}
// -->
</script>

<p><img name="samplepanda" src="https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/panda1.gif"></p>
<p>
<button type=button onclick="changeimg1()">パンダ1号に切り替え</button>
<button type=button onclick="changeimg2()">パンダ2号に切り替え</button>
</p>

↓↓↓

ブラウザ上の表示


■関連項目

document.images.length …… ドキュメント内にある画像の数を参照する
document.イメージ名.src …… 画像ファイルのURLを参照する
document.イメージ名.lowsrc …… 低解像度画像ファイルのURLを参照する
document.イメージ名.name …… 画像のname属性の値を参照する
document.イメージ名.complete …… 画像の読み込みが完了したかどうかを返す
document.イメージ名.width …… 画像の横幅を設定する
document.イメージ名.height …… 画像の高さを設定する
document.イメージ名.hspace …… 画像の水平方向の余白を設定する
document.イメージ名.vspace …… 画像の垂直方向の余白を設定する
document.イメージ名.border …… 画像のボーダーの太さを設定する
広告



Lorem Ipsum is simply dummy text

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.