简体   繁体   English

一旦从 select 下拉列表中选择了相应的选项,我就会尝试更改背景图像。 但这没有发生

[英]I am trying to change the background image as soon as the respective option is selected from the select drop down list. but this is not happening

 function bgimg() { var ch = document.getElementById("country").value; if (ch == "None") { document.getElementById("bgi").style.background = "black"; } else if (ch == "India") { document.getElementById("bgi").style.backgroundImage = "url('https://wallpaperaccess.com/full/5770.jpg')"; } else if (ch == "USA") { document.getElementById("bgi").style.backgroundImage = "url('Usa.jpghttps://wallpaperaccess.com/full/52904.jpg')"; } else if (ch == "Japan") { document.getElementById("bgi").style.backgroundImage = "url('https://wallpaperaccess.com/full/6512.jpg')"; } else { document.getElementById("bgi").style.backgroundImage = "url('https://wallpapercave.com/wp/wp1833702.jpg')"; } }
 <h1 align="center" style="font-family: umpush;">WELCOME TO INTERACTIVITY</h1> <div id="bgi"></div> <select id="country" onchange="Javascipt: bgimg()"> <option value="None">None</option> <option value="India">India</option> <option value="USA">USA</option> <option value="Israel">Israel</option> <option value="Japan">Japan</option> </select>

/* I am trying to change the background image as soon as the respective option is selected from the select drop down list. /* 一旦从 select 下拉列表中选择了相应的选项,我就会尝试更改背景图像。 but this is not happening.但这并没有发生。 OUTPUT: No background is being shown after choosing the options */ OUTPUT:选择选项后不显示背景 */

The Javascript code was put into the css textarea. Javascript 代码被放入 css 文本区域。 And there were some typos in your code ("Javascipt" and a wrong image url).您的代码中有一些拼写错误(“Javascipt”和错误的图片网址)。

Also he backgrounds were set on an empty div without any styling, which results in not seeing them.此外,他的背景设置在没有任何样式的空 div 上,导致看不到它们。 I have added a height and width on it so that you can at least see the backgrounds.我在上面添加了高度和宽度,以便您至少可以看到背景。 Furthermore I called the function at the start so that it will set the background for the initial value (which is "None" so the background becomes black)此外,我在开始时调用了 function 以便它将背景设置为初始值(即“无”,因此背景变为黑色)

 function bgimg() { var ch = document.getElementById("country").value; if (ch == "None") { document.getElementById("bgi").style.backgroundColor = "black"; } else if (ch == "India") { document.getElementById("bgi").style.backgroundImage = "url('https://wallpaperaccess.com/full/5770.jpg')"; } else if (ch == "USA") { document.getElementById("bgi").style.backgroundImage = "url('https://wallpaperaccess.com/full/52904.jpg')"; } else if (ch == "Japan") { document.getElementById("bgi").style.backgroundImage = "url('https://wallpaperaccess.com/full/6512.jpg')"; } else { document.getElementById("bgi").style.backgroundImage = "url('https://wallpapercave.com/wp/wp1833702.jpg')"; } } bgimg();
 #bgi { height: 200px; width: 300px; background-size: cover; }
 <h1 align="center" style="font-family: umpush;">WELCOME TO INTERACTIVITY</h1> <div id="bgi"></div> <select id="country" onchange="Javascript: bgimg()"> <option value="None">None</option> <option value="India">India</option> <option value="USA">USA</option> <option value="Israel">Israel</option> <option value="Japan">Japan</option> </select>

You can also add an event listener for change on Javascript if you want.如果需要,您还可以在 Javascript 上添加事件侦听器以进行更改。 Also, do not forget to give the #bgi div a width and height on CSS otherwise it won't show.另外,不要忘记在 CSS 上为#bgi div 指定宽度和高度,否则不会显示。

    
<h1 align="center" style="font-family: umpush;">WELCOME TO INTERACTIVITY</h1>
    <div id="bgi"></div>
    <select id="country">
        <option value="None">None</option>
        <option value="India">India</option>
        <option value="USA">USA</option>
        <option value="Israel">Israel</option>
        <option value="Japan">Japan</option>    
    </select> 


const select = document.querySelector("#country");

select.addEventListener('change', (event) => {
  const ch = document.getElementById("country").value;
  
  console.log(ch);
  
  if(ch=="None") {document.getElementById("bgi").style.background="black";}
  else if(ch=="India") {document.getElementById("bgi").style.backgroundImage="url('https://wallpaperaccess.com/full/5770.jpg')";}
  else if(ch=="USA") {document.getElementById("bgi").style.backgroundImage="url('Usa.jpghttps://wallpaperaccess.com/full/52904.jpg')";}
  else if(ch=="Japan") {document.getElementById("bgi").style.backgroundImage="url('https://wallpaperaccess.com/full/6512.jpg')";}
  else {document.getElementById("bgi").style.backgroundImage="url('https://wallpapercave.com/wp/wp1833702.jpg')";}

});

暂无
暂无

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

相关问题 我究竟做错了什么? 从下拉列表中获取选择。 Javascript - What am I doing wrong? Get selection from drop down list. Javascript 根据从其他组合框中选择的选项更改下拉选项 - Change the drop down option based on the selected option from other combobox 如何在选择下拉列表的选项中设置所选属性 - how to set selected attribute in option for select drop down list 未发生更改事件时选择下拉菜单 - select drop down menu on change event is not happening javascript更改选择选项背景颜色下拉 - javascript change select option background color drop down 如何将“ selected =&#39;selected&#39;”属性分配给特定 <option>在一个<select>清单。请只使用VanillaJS - How do I assign a “ selected='selected' ” attribute to a specific <option> in a <select> list. VanillaJS only please 在多选下拉列表中,我无法获取列表中空格后的所选值 - In multi select drop down I am not able to get the selected values after the spaces in the list 我试图确定变量是否与下拉框中选择的选项匹配。 我怎样才能做到这一点? - I am trying to determine whether a variable matches the option selected in the drop down box. How can I do this? 根据另一个下拉列表中的所选选项更改下拉列表中的项目 - Change items in a drop-down list depending on the selected option in another drop-down list 使用javascript在下拉列表中显示选定的选项 - Display a selected option from a drop down list into a box using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM