图片不会因为修改而改变"src"
Picture won't change by modifying "src"
好吧,我正在尝试编写另一个程序来练习我的 Javascript,但我遇到了另一个障碍。现在,程序很简单。 Select 来自种族列表的下拉列表(目前仅限于 "Swede" 和 "Italian"),然后程序将写下他们(刻板印象)外貌的简短描述,以及post 一张图片。或者更确切地说,更改默认 "Mystery man" 图片。
第一部分工作正常。文本会根据您 select "Swede" 或 "Italian." 而变化,图片部分不会。图片不会更改其默认 "Mystery man" 图片。这是为什么?
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">
document.getElementbyId("picture").src = ethnicities[documen...
应该是
document.getElementById("picture").src = ethnicities[documen...
您忘记在 'elementById'
中将 'b' 大写
这只是一个语法错误,不要忘记始终查看控制台以检测 problem.You 只需在“document.getElementById()”中写入大写 "B" 即可。
好吧,我正在尝试编写另一个程序来练习我的 Javascript,但我遇到了另一个障碍。现在,程序很简单。 Select 来自种族列表的下拉列表(目前仅限于 "Swede" 和 "Italian"),然后程序将写下他们(刻板印象)外貌的简短描述,以及post 一张图片。或者更确切地说,更改默认 "Mystery man" 图片。
第一部分工作正常。文本会根据您 select "Swede" 或 "Italian." 而变化,图片部分不会。图片不会更改其默认 "Mystery man" 图片。这是为什么?
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">
document.getElementbyId("picture").src = ethnicities[documen...
应该是
document.getElementById("picture").src = ethnicities[documen...
您忘记在 'elementById'
中将 'b' 大写这只是一个语法错误,不要忘记始终查看控制台以检测 problem.You 只需在“document.getElementById()”中写入大写 "B" 即可。