gpt4 book ai didi

javascript - 如何使用 javascript 中的 onchange 函数显示多张图片?

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

[Javascript][Html] 我通过下拉菜单获得了几个显示图片的 onchange 函数...例如:onchange A; onchange B;

如果我从 onchange B 函数中选择一张图片,A 的图片就会消失......那么如何解决这个问题呢?

    <!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap-css/css/bootstrap.min.css">
<link rel="stylesheet" href="Style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="bootstrap-css/js/bootstrap.min.js"></script>

<script type="text/javascript">
function displayImage(elem1) {
var image1 = document.getElementById("body");
image1.src = elem1.value;}

function displayImage(elem2) {
var image2 = document.getElementById("buttons");
image2.src = elem2.value;}

function displayImage(elem3) {
var image3 = document.getElementById("Dpad");
image3.src = elem3.value;}

function displayImage(elem4) {
var image4 = document.getElementById("Sticks");
image4.src = elem4.value;}

function displayImage(elem5) {
var image5 = document.getElementById("Top");
image5.src = elem5.value;}
</script>

</head>
<body>
<img src="Bilder/Custom/EmptyBody.png" alt="EmptyBody" class="i0">

<img id="body" class="i1">
<img id="buttons" class="i1">
<img id="Dpad" class="i1">
<img id="Sticks" class="i1">
<img id="Top" class="i1">

<select name="imageList1" onchange="displayImage(this);">
<option value="Bilder/Custom/Body/BlackBody.png">Black</option>
<option value="Bilder/Custom/Body/BlueBody.png">Blue</option>
<option value="Bilder/Custom/Body/GreenBody.png">Green</option>
<option value="Bilder/Custom/Body/OrangeBody.png">Orange</option>
<option value="Bilder/Custom/Body/RedBody.png">Red</option>
<option value="Bilder/Custom/Body/WhiteBody.png">White</option>
<option value="Bilder/Custom/Body/YellowBody.png">Yellow</option>
<option value="Bilder/Custom/Body/RainbowBody.png">Rainbow</option>
</select>

<select name="imageList2" onchange="displayImage(this);">
<option value="Bilder/Custom/Buttons/StandardButtons.png">Standard</option>
<option value="Bilder/Custom/Buttons/360Buttons.png">360-Buttons</option>
</select>

<select name="imageList3" onchange="displayImage(this);">
<option value="Bilder/Custom/Dpad/BlackDPad.png">Black</option>
<option value="Bilder/Custom/Dpad/WhiteDPad.png">White</option>
</select>

<select name="imageList4" onchange="displayImage(this);">
<option value="Bilder/Custom/Sticks/BlackSticks.png">Black</option>
<option value="Bilder/Custom/Sticks/WhiteSticks.png">White</option>
</select>

<select name="imageList5" onchange="displayImage(this);">
<option value="Bilder/Custom/Top/BlackTop.png">Black</option>
<option value="Bilder/Custom/Top/BlueTop.png">Blue</option>
<option value="Bilder/Custom/Top/GreenTop.png">Green</option>
<option value="Bilder/Custom/Top/OrangeTop.png">Orange</option>
<option value="Bilder/Custom/Top/RedTop.png">Red</option>
<option value="Bilder/Custom/Top/WhiteTop.png">White</option>
<option value="Bilder/Custom/Top/YellowTop.png">Yellow</option>
</select>
</body>
</html>

最佳答案

这是一个例子:

function displayImage(el,target) {
var imageTarget = document.getElementById(target);
imageTarget.src = el.value;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap-css/css/bootstrap.min.css">
<link rel="stylesheet" href="Style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="bootstrap-css/js/bootstrap.min.js"></script>
</head>
<body>
<img src="Bilder/Custom/EmptyBody.png" alt="EmptyBody" class="i0">

<img id="body" class="i1">
<img id="buttons" class="i1">
<img id="Dpad" class="i1">
<img id="Sticks" class="i1">
<img id="Top" class="i1">

<select name="imageList1" onchange="displayImage(this,'body');">
<option value="Bilder/Custom/Body/BlackBody.png">Black</option>
<option value="Bilder/Custom/Body/BlueBody.png">Blue</option>
<option value="Bilder/Custom/Body/GreenBody.png">Green</option>
<option value="Bilder/Custom/Body/OrangeBody.png">Orange</option>
<option value="Bilder/Custom/Body/RedBody.png">Red</option>
<option value="Bilder/Custom/Body/WhiteBody.png">White</option>
<option value="Bilder/Custom/Body/YellowBody.png">Yellow</option>
<option value="Bilder/Custom/Body/RainbowBody.png">Rainbow</option>
</select>

<select name="imageList2" onchange="displayImage(this,'buttons');">
<option value="Bilder/Custom/Buttons/StandardButtons.png">Standard</option>
<option value="Bilder/Custom/Buttons/360Buttons.png">360-Buttons</option>
</select>

<select name="imageList3" onchange="displayImage(this,'Dpad');">
<option value="Bilder/Custom/Dpad/BlackDPad.png">Black</option>
<option value="Bilder/Custom/Dpad/WhiteDPad.png">White</option>
</select>

<select name="imageList4" onchange="displayImage(this,'Sticks');">
<option value="Bilder/Custom/Sticks/BlackSticks.png">Black</option>
<option value="Bilder/Custom/Sticks/WhiteSticks.png">White</option>
</select>

<select name="imageList5" onchange="displayImage(this,'Top');">
<option value="Bilder/Custom/Top/BlackTop.png">Black</option>
<option value="Bilder/Custom/Top/BlueTop.png">Blue</option>
<option value="Bilder/Custom/Top/GreenTop.png">Green</option>
<option value="Bilder/Custom/Top/OrangeTop.png">Orange</option>
<option value="Bilder/Custom/Top/RedTop.png">Red</option>
<option value="Bilder/Custom/Top/WhiteTop.png">White</option>
<option value="Bilder/Custom/Top/YellowTop.png">Yellow</option>
</select>
</body>
</html>

关于javascript - 如何使用 javascript 中的 onchange 函数显示多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753768/

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