<div id="change" style="height:20px; width:100%; position: absolute; float:bottom; background-color:#000000">
</div> <br>
<select name="bgcolor" id="bgcolor" onchange="colorDiv()">
<option class="1" value=1> Grey
<option class="2" value=2> White
<option class="3" value=3> Blue
<option class="4" value=4> Cian
<option class="5" value=5> Green
</select> <br><br>
<p id="demo"></p>
<script>
function colorDiv(){
var selection = document.getElementById('bgcolor');
var div = document.getElementById('change');
div.style.backgroundColor='green';
document.getElementById("demo").innerHTML =selection;
switch (selection){
case 1:
div.style.backgroundColor='grey';
case 2:
div.style.backgroundColor='white';
case 3:
div.style.backgroundColor='blue';
case 4:
div.style.backgroundColor='cian';
case 5:
div.style.backgroundColor='green';
}
</script>
嗨!我试图用js改变div的背景颜色,但是它没有检测到选定的值,正如我在打印它时看到的那样。我在多个页面中看到了这个过程,它对我来说看起来是一样的,但它实际上不适用于我的代码。你能看到任何错误吗? 谢谢!使用javascript更改div color
我把复制你的修改代码到您的文章的自由。 JSFiddles很好,但请在你的回答中加入代码。 – George
@oGeez是的,我同意,应该包括它自己。谢谢 –
@ArtyomNeustroev使用'cyan'而不是'cian'。 – TheGreenOmega