html Seleccionar com uma cor de fundo diferente para cada opção que funciona correctamente em cada navegador?
Estou à procura de criar vários elementos de selecção html com cores de fundo personalizadas para cada opção:
<select runat="server" id="select">
<option value="A">White</option>
<option value="B">Red</option>
<option value="C">Yellow</option>
<option value="D">Green</option>
Quando a página web carregar, gostaria que o elemento select mostrasse apenas a cor de fundo e sem texto para a opção seleccionada. O texto (branco, vermelho,....) só deve ser visível quando a queda é aberta.
à medida que o valor seleccionado é alterado pelo utilizador, a cor de fundo também deverá mudar, enquanto o texto deverá ser invisível na desistência fechada.
seria muito bom se a solução funcionar na maioria dos navegadores, incluindo o IE 9/10.
Saúde. 8
4 answers
Tente este código, funciona em todos os navegadores, incluindo IE:
Html
<select id="select1" onchange="colourFunction()">
<option class="white" value="A">This should have a WHITE background</option>
<option class="red" value="B">This should have a RED background</option>
<option class="yellow" value="C">This should have a YELLOW background</option>
<option class="green" value="D">This should have a GREEN background</option>
</select>
Css
#select1 {width:150px; color:rgba(0, 0, 0, 0);}
#select1:focus, #select1:focus {
color:black;
}
.white {background:white;}
.red {background:red;}
.yellow {background:yellow;}
.green {background:green}
Js
function colourFunction() {
var myselect = document.getElementById("select1"),
colour = myselect.options[myselect.selectedIndex].className;
myselect.className = colour;
myselect.blur(); //This just unselects the select list without having to click
somewhere else on the page
}
HTH:)
9
Author: Singh, 2013-05-13 22:25:06
Por favor, dê uma olhada no seguinte jsfiddle
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<select runat="server" id="select">
<option value="A" style="background-color: white;">White</option>
<option value="B" style="background-color: red;">Red</option>
<option value="C" style="background-color: yellow;">Yellow</option>
<option value="D" style="background-color: green;">Green</option>
</select>
<script>
$('#select').change(function(){
if($(this).val() == 'A'){
$("body").css('background-color', 'white');
}
if($(this).val() == 'B'){
$("body").css('background-color', 'red');
}
if($(this).val() == 'C'){
$("body").css('background-color', 'yellow');
}
if($(this).val() == 'D'){
$("body").css('background-color', 'green');
}
});
</script>
</body>
</html>
0
Author: AgeDeO, 2013-05-13 12:39:16
<script>
function changecolor(id,color){
id.style.backgroundColor=color;
}
</script>
<div id="container">
<p> Select Color to change background:</p>
<select id="themenu" onchange="changecolor(container,value)">
<option value="white"> </option>
<option value="red">RED</option>
<option value="blue">BLUE</option>
<option value="green">GREEN</option>
</select>
</div>
0
Author: Katyoshah, 2013-12-26 10:06:56
Isso é praticamente o mesmo que @Singh com algumas pequenas mudanças para torná-lo um pouco mais flexível, ele permite que você tenha várias seleções mudar de cores.
CSS
<style>
.red {
color: darkred;
background-color: red;
}
.purple {
color: darkmagenta;
background-color: magenta;
}
.yellow {
color: darkkhaki;
background-color: yellow;
}
.aqua {
color: mediumaquamarine;
background-color: aqua;
}
.blue {
color: lightblue;
background-color: blue;
}
.green {
color: lightgreen;
background-color: green;
}
</style>
Html
<select name="pos1" id="pos1" onchange="colourFunction(this)">
<option disabled selected value>select</option>
<option class="red" value="1">Red</option>
<option class="purple" value="2">Purple</option>
<option class="yellow" value="3">Yellow</option>
<option class="aqua" value="4">Aqua</option>
<option class="blue" value="5">Blue</option>
<option class="green" value="6">Green</option>
</select>
<select name="pos2" id="pos2" onchange="colourFunction(this)">
<option disabled selected value>select</option>
<option class="red" value="1">Red</option>
<option class="purple" value="2">Purple</option>
<option class="yellow" value="3">Yellow</option>
<option class="aqua" value="4">Aqua</option>
<option class="blue" value="5">Blue</option>
<option class="green" value="6">Green</option>
</select>
JS
<script>
function colourFunction(pos) {
pos.className = pos[pos.selectedIndex].className;
pos.blur();
}
</script>
0
Author: Pete, 2016-08-18 05:44:04