2

我有 javascript 的问题,很抱歉,但我不擅长 Javascript

我正在尝试在其他元素上运行 cnagecolor,并传递我想要的颜色,但没有运气:(

这就是我现在所拥有的

<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>
4

4 回答 4

3

尝试类似...

<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>
于 2013-10-08T10:27:12.257 回答
0

尝试这个

col.style.backgroundColor = i;
于 2013-10-08T10:28:07.360 回答
0

选项标签不支持“onclick”事件(以及“onselect”) 。因此,您可以改用“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>
于 2013-10-08T10:38:00.640 回答
0

你可以做这样的事情..

<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>
于 2013-10-08T10:38:52.270 回答