2017-09-29 102 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」即可。