简体   繁体   English

如何使用javascript使用select标签更改div的背景图像?

[英]How to change background image of div with select tag, using javascript?

How can i change the background image of my div using the select tag so when a certain option is selected the background changes, this is my code: 我如何使用select标签更改div的背景图像,所以当选择某个选项时,背景会发生变化,这是我的代码:

<div class="idcard" id="idcard-div"></div>
<div class="select-idcard" id="idcard-select">
        <p>Select Background Image</p>
        <select>
            <option id="idcard-option1" value="blue">Default/Blue</option>
            <option id="idcard-option2" value="red">Red</option>
            <option id="idcard-option3" value="purple">Purple</option>
            <option id="idcard-option4" value="green">Green</option>
        <select>
    </div>

Javascript code: JavaScript代码:

var changeBgColor = function(){
        var idCardBg = document.getElementById("idcard-div");
        if(document.getElementById("idcard-option1").value === "blue"){
            idCardBg.style.backgroundImage = "url('tigerbg.jpg')";
        }else if(document.getElementById("idcard-option2").value === "red"){
            idCardBg.style.backgroundImage = "url('redbg.jpg')";
        }else if(document.getElementById("idcard-option3").value === "purple"){
            idCardBg.style.backgroundImage = "url('purplebg.jpg')";
        }else if(document.getElementById("idcard-option3").value === "green"){
            idCardBg.style.backgroundImage = "url('greenbg.jpg')";
        }else{
            idCardBg.style.backgroundImage = "url('tigerbg.jpg')";
        }
    };

.. ..

changeBgColor();

You've made couple of mistakes. 您犯了几个错误。

First of all, the ids don't match, and you used the attribute class instead of id on the div. 首先,id不匹配,您在div上使用了属性class而不是id

Also, you should get the value of the select element, not of its options. 另外,您应该获得select元素的值,而不是其选项的值。

And lastly, you must of course attach the event to the select element. 最后,您当然必须将事件附加到select元素。 I've done that in HTML in the snippet below, but you can do it in JavaScript too, using the onchange property or using addEventListener . 我已经在下面的代码段中用HTML完成了此操作,但是您也可以使用onchange属性或addEventListener在JavaScript中完成此操作。

So, besides a well-meant advice to follow a tutorial on events, I would like to suggest you to have a look at the developer tools in your browser. 因此,除了一个很好的建议来遵循事件教程之外,我还建议您浏览一下浏览器中的开发人员工具。 They contain a JavaScript console that shows errors if code is run. 它们包含一个JavaScript控制台,如果运行代码,该控制台将显示错误。 So as soon as you attached the event, you can have a look at the console to find the other errors. 因此,一旦您附加了该事件,便可以查看控制台以查找其他错误。

Anyway, this time I've done that for you. 无论如何,这次我为您做了。 Below is the fixed snippet. 以下是固定的代码段。 I also use the value to set a background color, so you can roughly see it working without having access to the actual images. 我还使用该值设置背景色,因此您可以粗略地看到它的工作状态,而无需访问实际图像。

 var changeBgColor = function(){ var idCardBg = document.getElementById("idcard"); idCardBg.style.backgroundImage = "url('tigerbg.jpg')"; if(document.getElementById("select-idcard").value === "blue"){ idCardBg.style.backgroundImage = "url('tigerbg.jpg')"; }else if(document.getElementById("select-idcard").value === "red"){ idCardBg.style.backgroundImage = "url('redbg.jpg')"; }else if(document.getElementById("select-idcard").value === "purple"){ idCardBg.style.backgroundImage = "url('purplebg.jpg')"; }else if(document.getElementById("select-idcard").value === "green"){ idCardBg.style.backgroundImage = "url('greenbg.jpg')"; }else{ idCardBg.style.backgroundImage = "url('tigerbg.jpg')"; } idCardBg.style.backgroundColor = document.getElementById("select-idcard").value; }; 
 <div id="idcard"> <p>Select Background Image</p> <select onchange="changeBgColor();" id="select-idcard"> <option id="idcard-option1" value="blue">Default/Blue</option> <option id="idcard-option2" value="red">Red</option> <option id="idcard-option3" value="purple">Purple</option> <option id="idcard-option4" value="green">Green</option> </select> </div> 

HTML changes HTML更改

<select id="colorSelect" onchange="changeBgColor();">
  <option value="blue">blue</option>
  <option value="red">red</option>
</select>

JS changes (using select.options array and its value) JS更改(使用select.options数组及其值)

var changeBgColor = function(){
  var idCardBg = document.getElementById("idcard-select");
  var colorSelect = document.getElementById("colorSelect");
  switch (colorSelect.options[colorSelect.selectedIndex].value)
  {
    case 'blue': idCardBg.style.backgroundImage = "url('bluebg.jpg')"; break;
    case 'red': idCardBg.style.backgroundImage = "url('redbg.jpg')"; break;
    default:
      idCardBg.style.backgroundImage = "url('tigerbg.jpg')";
    break;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM