2017-07-07 83 views
-1

将脚本放到裸露的骨骼上,我遵循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动画,这是我在这里的终极目标。任何帮助,将不胜感激。

+5

也许'.scr'应该是'.src' – ASDFGerte

+1

变化'的document.getElementById( “indexPhoto”)。scr'到'的document.getElementById( “indexPhoto”)。src'。 –

回答

3

您有一个错字,请将scr更改为src

0

在JavaScript:

var ele=document.getElementById("indexPhoto"); 
ele.setAttribute('src',"landscape.jpg"); 
1

document.getElementById("indexPhoto").scr应该是:

document.getElementById("indexPhoto").src 
0

这是语法错误,你应该使用document.getElementById("indexPhoto").src而不是document.getElementById("indexPhoto").scr,它只有一个字母的问题。