简体   繁体   English

更改元素的背景颜色?

[英]Change background color of element?

I have issues with javascript, sorry for this but i am not good in Javascript 我在使用javascript时遇到问题,对此感到抱歉,但我的Java语言不佳

I am trying, to function cnagecolor on other element, and pass color what i want, but no luck :( 我正在尝试在其他元素上使用cnagecolor,并传递我想要的颜色,但没有运气:(

This is what i have for now 这就是我现在所拥有的

<script>
function changeColor(i) {
    var col = document.getElementById("changecolor");
    col.style.backgroundColor = '+i+';
}
</script>
<select id="changecolor">
<option>Kategorija</option>
<option onclick="changeColor('rgb(51,51,51)');" style="background-color:rgb(51,51,51);"></option>
<option onclick="changeColor('rgb(0,51,255)');" style="background-color:rgb(0,51,255);"></option>
<option onclick="changeColor('rgb(204,0,153)');" style="background-color:rgb(204,0,153)"></option>
<option onclick="changeColor('rgb(255,204,153)');" style="background-color:rgb(255,204,153)"></option>
</select>

try something like... 尝试类似...

<script>
    function changeColor() {
        var col = document.getElementById("changecolor");
        col.style.backgroundColor = col.value;
    }
</script>

 <select onchange="changeColor()" id="changecolor">
<option >Kategorija</option>
<option value="rgb(51,51,51)" style="background-color:rgb(51,51,51);"></option>
<option value="rgb(0,51,255)" style="background-color:rgb(0,51,255);"></option>
<option value="rgb(204,0,153)" style="background-color:rgb(204,0,153)"></option>
<option value="rgb(255,204,153)" style="background-color:rgb(255,204,153)"></option>
</select>

尝试这个

col.style.backgroundColor = i;

'onclick' event (and 'onselect' too) isn't supported by the option tag. 选项标记不支持“ onclick”事件(以及“ onselect”事件)。 So, you can use 'onchange' instead: 因此,您可以改用“ onchange”:

function select_onChange() {
   var selectElem = document.getElementById("changecolor");
   selectElem.style.backgroundColor = selectElem.value;
}

<select id="changecolor" onchange="select_onChange()">
   <option value="rgb(51,51,51)" style="background-color:rgb(51,51,51);">Option #1</option>
   <option value="rgb(0,51,255)" style="background-color:rgb(0,51,255);">Option #2</option>
</select>

You can do something like this.. 你可以做这样的事情。

<script>
    // Execute this function on change of the select
    function changeColor() {
    var col = document.getElementById("changecolor");
    // Get the value of selected option in select
    var values = col.options[col.selectedIndex].value;
    col.style.backgroundColor = values;
}

</script>

<select id="changecolor" onchange="changeColor();">
<option>Kategorija</option>
<option value="rgb(51,51,51)">a</option>
<option value="rgb(0,51,255)">v</option>
<option value="rgb(204,0,153)">s</option>
<option value="rgb(255,204,153)">d</option>
</select>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM