将脚本放到裸露的骨骼上,我遵循w3schools.com(https://www.w3schools.com/js/js_htmldom_html.asp)的示例:document.getElementById(id).attribute = new value。他们的例子:使用javascript更改属性的值
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
这是我的。我在Chrome控制台上没有收到错误消息。
<body>
<img id = "indexPhoto" src =
"newthaiimages/indexPhotos/indexPhoto_320.jpg"
width="320" height="474" alt="hero sepia toned photo of Thai
temple next to a river"/>
<script>
document.getElementById("indexPhoto").scr =
"newthaiimages/indexPhotos/indexPhoto_450.jpg";
</script>
</body>
你可以在http://newthaimassage.com/index3.php看到结果。这是脚本没有被剥离。
<script>
window.onload = function() {
var w = window
var x = w.innerWidth
switch(w.innerWidth){
case x <= 320:
break;
case x <= 768:
document.getElementById("indexPhoto").scr ="indexPhoto_450.jpg";
break;
case x <= 1024:
document.getElementById("indexPhoto").scr ="indexPhoto_835.jpg";
break;
case x <= 1420:
document.getElementById("indexPhoto").scr ="indexPhoto_1000.jpg";
break;
default:
document.getElementById("indexPhoto").scr ="indexPhoto_1200.jpg";
}}
</script>
目前我使用CSS媒体查询显示的窗口大小的图像,但使用背景图片,我不能使用CSS动画,这是我在这里的终极目标。任何帮助,将不胜感激。
也许'.scr'应该是'.src' – ASDFGerte
变化'的document.getElementById( “indexPhoto”)。scr'到'的document.getElementById( “indexPhoto”)。src'。 –