2017-09-29 60 views
-1

好的,我正在尝试编写另一个程序来练习我的Javascript,并且我遇到了另一个障碍。目前,该计划很简单。从种族列表(当前仅限于“瑞典人”和“意大利人”)的下拉列表中进行选择,然后该程序将写出他们(刻板印象)外观的简短描述,并张贴图片。或者说,更改默认的“神秘人”图片。通过修改“src”不会改变图片

第一部分正常工作。如果您选择“瑞典人”或“意大利语”,文字会有所不同。图片部分没有。图像不会改变其默认的“神秘人”图片。这是为什么?

var ethnicities = [{ 
 
    name: "Swede", 
 
    eyecolor: "blue", 
 
    hairtex: "straight", 
 
    fp: 1, 
 
    pic: "Swedish.png" 
 
    }, 
 
    { 
 
    name: "Italian", 
 
    eyecolor: "brown", 
 
    hairtex: "curly", 
 
    fp: 2, 
 
    pic: "Italian.png" 
 
    } 
 
]; 
 

 

 
function description() { 
 
    var desc = "The " + ethnicities[document.EPF.EPDD.value].name + 
 
    " is " + ethnicities[document.EPF.EPDD.value].eyecolor + "-eyed and " + 
 
    ethnicities[document.EPF.EPDD.value].hairtex + "-haired." + 
 
    ""; 
 

 

 

 
    document.getElementById("demo").innerHTML = desc; 
 
    document.getElementbyId("picture").src = ethnicities[document.EPF.EPDD.value].pic; 
 
    //so weird, it just doesn't chnage it. It doesn't matter what I put on the 
 
    //right of the equal sign. 
 
}
<p>Ethnicity presets:</p> 
 
<form name="EPF"> 
 
    <select name="EPDD"> 
 
    <option value="0">Swede</option> 
 
    <option value="1">Italian</option> 
 
    
 
    </select> 
 

 
    <input type="button" value="Submit" onClick="description()"> 
 

 
</form> 
 
<p id="demo"> </p> 
 

 
<img id="picture" src="Mystery man.png" alt="unknown">

+9

错字,'getElementbyId'应该是'getElementById' – adeneo

+0

哦,我的天哪,谢谢!令人惊奇的是,这些小东西让你编程! –

+1

您应该删除它。 – Rob

回答

0
document.getElementbyId("picture").src = ethnicities[documen... 

应该

document.getElementById("picture").src = ethnicities[documen... 

你忘了大写的 'B' 'elementById'

0

这仅仅是语法错误,不要总是忘记看控制台。 您只需在“document.getElementById()”中写入大写“B”即可。