如何在javascript中使用onchange功能显示多张图片?

时间:2017-05-03 07:27:29

标签: javascript html

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

3 个答案:

答案 0 :(得分:0)

问题是你有5个同名的功能。 当您致电displayImage()时,脚本会找到第一个名称为

的功能

解决方案重命名您的功能

Js不会过滤参数名称,只能过滤参数数量

替换你的身份:像这样

<html>
<head>
   
    <script>
      function displayImage(elem){
        var image1 = document.getElementById("body");
        image1.src = elem1.value;
      }
    </script>
</head>

<body >
    <img src="img.jpg" 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="img.jpg">Black</option>
      <option value="img.jpg">Blue</option>
      <option value="img.jpg">Green</option>
      <option value="img.jpg">Orange</option>
      <option value="img.jpg">Red</option>
      <option value="img.jpg">White</option>
      <option value="img.jpg">Yellow</option>
      <option value="img.jpg">Rainbow</option>
</select>

    <select name="imageList2" onchange="displayImage(this);">
      <option value="img.jpg">Standard</option>
      <option value="img.jpg">360-Buttons</option>
</select>

    <select name="imageList3" onchange="displayImage(this);">
      <option value="img.jpg">Black</option>
      <option value="img.jpg">White</option>
</select>

    <select name="imageList4" onchange="displayImage(this);">
      <option value="img.jpg">Black</option>
      <option value="img.jpg">White</option>
</select>

    <select name="imageList5" onchange="displayImage(this);">
      <option value="img.jpg">Black</option>
      <option value="img.jpg">Blue</option>
      <option value="img.jpg">Green</option>
      <option value="img.jpg">Orange</option>
      <option value="img.jpg">Red</option>
      <option value="img.jpg">White</option>
      <option value="img.jpg">Yellow</option>
</select>
</body>
</html>

答案 1 :(得分:0)

以下是一个例子:

&#13;
&#13;
function displayImage(el,target) {
  var imageTarget = document.getElementById(target);
  imageTarget.src = el.value;
}
&#13;
<!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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这里不需要多次使用相同的功能,您可以使用以下代码

<!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, elem2) {
        var image1 = document.getElementById(elem2);
        image1.src = elem1.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, '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>