gpt4 book ai didi

javascript - 从下拉列表中选择选项时显示图像

转载 作者:行者123 更新时间:2023-11-28 03:03:18 25 4
gpt4 key购买 nike

因此,当选择一个选项时,ID为“ phone”的图像应根据选择的选项而变化

HTML:

<div id="form1">

<img id="phone" src="iphone6.jpg" width="294" height="320"/>
<select id="phoneList">
<option value="iphone1.jpg">Apple iPhone 11 Pro Max (2019) 512GB Silver</option>
<option value="iphone2.jpg">Apple iPhone 11 (2019) 128GB Black</option>
<option value="iphone3.jpg">Apple iPhone 11 (2019) 256GB White</option>
</select>
</div>


Javascript:

function setPhone() {
var img = document.getElementById("phone");
img.src = this.value;
return false;
}
document.getElementById("phoneList").onchange = setPhone;

最佳答案

您可能按照注释中的建议编写了错误的路径(在这种情况下,图像应与HTML文件位于同一目录中)或放错了脚本部分。这对我来说很好:

<div id="form1">
<img id="phone" src="iphone6.jpg" width="294" height="320"/>
<select id="phoneList">
<option value="iphone1.jpg">Apple iPhone 11 Pro Max (2019) 512GB Silver</option>
<option value="iphone2.jpg">Apple iPhone 11 (2019) 128GB Black</option>
<option value="iphone3.jpg">Apple iPhone 11 (2019) 256GB White</option>
</select>
</div>
<script>
function setPhone() {
var img = document.getElementById("phone");
img.src = this.value;
return false;
}
document.getElementById("phoneList").onchange = setPhone;
</script>

关于javascript - 从下拉列表中选择选项时显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60804851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com