使用javascript更改div颜色 灰色 白色 蓝色 奇安 绿色 函数colorDiv(){ var selection=document.getElementById('bgcolor'); var div=document.getElementById('change'); div.style.backgroundColor='green'; document.getElementById(“demo”).innerHTML=selection; 开关(选择){ 案例1: div.style.backgroundColor='grey'; 案例2: div.style.backgroundColor='white'; 案例3: div.style.backgroundColor='blue'; 案例4: div.style.backgroundColor='cian'; 案例5: div.style.backgroundColor='green'; }
您好!我正试图用js更改div的背景色,但它没有检测到所选的值,正如我在parragraph上打印它时所看到的。我在多个页面中看到了这个过程,它看起来对我来说是一样的,但它实际上对我的代码不起作用。您能看到任何错误吗? 谢谢使用javascript更改div颜色 灰色 白色 蓝色 奇安 绿色 函数colorDiv(){ var selection=document.getElementById('bgcolor'); var div=document.getElementById('change'); div.style.backgroundColor='green'; document.getElementById(“demo”).innerHTML=selection; 开关(选择){ 案例1: div.style.backgroundColor='grey'; 案例2: div.style.backgroundColor='white'; 案例3: div.style.backgroundColor='blue'; 案例4: div.style.backgroundColor='cian'; 案例5: div.style.backgroundColor='green'; },javascript,html,Javascript,Html,您好!我正试图用js更改div的背景色,但它没有检测到所选的值,正如我在parragraph上打印它时所看到的。我在多个页面中看到了这个过程,它看起来对我来说是一样的,但它实际上对我的代码不起作用。您能看到任何错误吗? 谢谢 关闭选项标记 使用document.getElementById('bgcolor')。值 不要忘了在每个案例中添加break,否则每次都会出现绿色div 将字符串用于案例条件 修订的Javascript: <div id="change" style="heigh
选项
标记document.getElementById('bgcolor')。值
案例中添加break
,否则每次都会出现绿色div
案例
条件 <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>
这就是总结。此外,开关盒需要在每一个开关盒后都有一个break语句,否则最后一个开关盒将是您将看到的开关盒,在本例中为绿色。
改变
灰色
白色
蓝色
奇安
绿色
函数colorDiv(){
var selection=document.getElementById('bgcolor')。selectedIndex;
var div=document.getElementById('change');
div.style.backgroundColor='green';
var颜色=[“灰色”、“白色”、“蓝色”、“彩色”、“绿色”];
document.getElementById(“demo”).innerHTML=colors[selection];
开关(选择){
案例0:
div.style.backgroundColor='grey';
打破
案例1:
div.style.backgroundColor='white';
打破
案例2:
div.style.backgroundColor='blue';
打破
案例3:
div.style.backgroundColor='cian';
打破
案例4:
div.style.backgroundColor='green';
打破
违约:
div.style.backgroundColor='purple';
}
};
试试看
函数myFunction(){
var latters='ABCDEF1234567890'。拆分(“”);
var color='#';
对于(变量i=0;i<6;i++)
{
颜色+=latters[Math.floor(Math.random()*16)];
}
document.getElementById(“demo”).style.background=color;
}
我冒昧地将您修改后的代码复制到您的帖子中。JSFiddles很好,但请在您的答案中也包含代码。@oGeez是的,我同意,我应该自己包含它。谢谢you@ArtyomNeustroev使用cyan
而不是cian
。您的答案只包含代码。您应该实际解释代码的作用为OP提供一个好的答案!
function colorDiv() {
var selection = document.getElementById('bgcolor').value;
var div = document.getElementById('change');
div.style.backgroundColor = 'green';
document.getElementById("demo").innerHTML = selection;
switch (selection) {
case "1":
div.style.backgroundColor = 'grey';
break;
case "2":
div.style.backgroundColor = 'white';
break;
case "3":
div.style.backgroundColor = 'blue';
break;
case "4":
div.style.backgroundColor = 'cian';
break;
case "5":
div.style.backgroundColor = 'green';
break;
}
}
<div id="change" style="height:20px; background-color:#000000">
change
</div>
<select name="bgcolor" id="bgcolor" onchange="colorDiv()">
<option class="1" value="0"> Grey</option>
<option class="2" value="1"> White </option>
<option class="3" value="2"> Blue</option>
<option class="4" value="3"> Cian</option>
<option class="5" value="4"> Green</option>
</select>
<p id="demo"></p>
<script>
function colorDiv(){
var selection = document.getElementById('bgcolor').selectedIndex;
var div = document.getElementById('change');
div.style.backgroundColor='green';
var colors = ["grey","white","blue","cian","green"];
document.getElementById("demo").innerHTML = colors[selection];
switch (selection){
case 0:
div.style.backgroundColor='grey';
break;
case 1:
div.style.backgroundColor='white';
break;
case 2:
div.style.backgroundColor='blue';
break;
case 3:
div.style.backgroundColor='cian';
break;
case 4:
div.style.backgroundColor='green';
break;
default:
div.style.backgroundColor='purple';
}
};
</script>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<div style="width:100px; height:100px; float:left;border:1px solid #000;" id="demo"></div>
<script>
function myFunction() {
var latters = 'ABCDEF1234567890'.split("");
var color = '#';
for (var i=0; i < 6; i++)
{
color+=latters[Math.floor(Math.random() * 16)];
}
document.getElementById("demo").style.background = color ;
}
</script>
</body>
</html>