次のようにパスを返す JavaScript 関数を呼び出して、「src」を設定しようとしています。
<img src="getImagePath()" />
関数:
function getImagePath(){
return "images/image1.png";
}
しかし、うまくいかないようです。不足しているものはありますか?私を正しい方向に向けてくれた人に感謝します。
次のようにパスを返す JavaScript 関数を呼び出して、「src」を設定しようとしています。
<img src="getImagePath()" />
関数:
function getImagePath(){
return "images/image1.png";
}
しかし、うまくいかないようです。不足しているものはありますか?私を正しい方向に向けてくれた人に感謝します。
このsrc
属性は、JavaScript ではなく URL を取ります。あなたは試してみたいかもしれません
<img src="pixel.gif" onload="this.onload=null; this.src=getImagePath();" />
これはできません。要素のsrc
属性はimg
、html が解釈されるときに javascript として解釈できません。
しかし、これを行うことができます:
<img id=someImage>
<script>
function getImagePath(){
return "images/image1.png";
}
document.onload = function(){
document.getElementById('someImage').src=getImagePath();
};
</script>