在src中通过javascript更改img src存储在数组中

时间:2014-12-16 10:45:49

标签: javascript html

我试图在页面加载时更改图像的来源而不是图像本身加载,正在显示它的位置字符串。很可能是一个简单的解决方案,但我看不到它。

<form id="form1"><h1 id="dude">question?</h1><br>
                    <h2>
                        <label for ="answer1" id="label1"><img src="imgs//img.png" id="img1" width="150" height = "150"></label>
                        <input type="radio" name="answer" id="answer1">
                    </h2><br>
                    <h2>
                        <label for ="answer2" id="label2"><img src="imgs//img.png" id="img2" width="150" height = "150"></label>
                        <input type="radio" name="answer" id="answer2">
                    </h2><br>
                    <h2>
                        <label for="answer3" id="label3"><img src="imgs//img.png" id="img3" width="150" height = "150"></label>
                        <input type="radio" name="answer" id="answer3">
                    </h2>
                    <h2>
                        <label for="answer4" id="label4"><img src="imgs//img.png" id="img4" width="150" height = "150"></label>
                        <input type="radio" name"answer" id="answer4">
                    </h2>
                </form>

和javascript

//  populate mulChoiImg array   
mulChoiImg[1] = new Array('Which picture do you like most', 'imgs//img2.png','imgs//img2.png','imgs//img2.png','imgs//img2.png');
function popMulC(){
                document.getElementById("dude").innerHTML= mulchoi[currentQuestion][0];
                document.getElementById("label1").innerHTML=mulchoi[currentQuestion][1];
                document.getElementById("label2").innerHTML=mulchoi[currentQuestion][2];
                document.getElementById("label3").innerHTML=mulchoi[currentQuestion][3];
                document.getElementById("label4").innerHTML=mulchoi[currentQuestion][3];}//close func

任何想法都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您实际上并没有更改图像src,而只是更改Label元素的innerHtml。

您需要从标签中获取图像元素,然后执行image.src = mulchoi[currentQuestion][1];

答案 1 :(得分:0)

您可以使用此

 var mulChoiImg = new Array('Which picture do you like most', 'imgs/img2.png','imgs/img2.png','imgs//img2.png','imgs/img2.png');

function popMulC(){
 document.getElementById("label1").innerHTML = "<img src=" + mulChoiImg[1] + "/>";
 }