[英]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.