通过修改“src”不会改变图片

时间:2017-09-29 10:53:30

标签: javascript html image dom src

好吧,我正在尝试编写另一个程序来练习我的Javascript,而且我遇到了另一个障碍。目前,该计划很简单。从种族列表(目前仅限于“瑞典语”和“意大利语”)的下拉列表中选择,然后程序将写出其(陈规定型)外观的简短描述,并张贴图片。或者更确切地说,更改默认的“神秘人”图片。

第一部分工作正常。文本的更改取决于您选择“瑞典语”还是“意大利语”。图片部分没有。图像不会改变其默认的“神秘人”图片。那是为什么?

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">

2 个答案:

答案 0 :(得分:0)

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

应该是

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

您忘了将'elementById'中的'b'大写

答案 1 :(得分:0)

这只是一个语法错误,不要忘记总是查看控制台来检测问题。你只需在“document.getElementById()”中写下大写“B”。