Jquery 使用onclick.getElementById()更改颜色。style.Color仅在Firefox中有效?

Jquery 使用onclick.getElementById()更改颜色。style.Color仅在Firefox中有效?,jquery,select,colors,html-table,onclick,Jquery,Select,Colors,Html Table,Onclick,我有一个表,我想在其中使用以下代码更改第TH行的文本颜色: <select> <option onclick="document.getElementById('tablehead').style.color=''">Default</option> <option onclick="document.getElementById('tablehead').style.color='red'">Red</option> &l

我有一个表,我想在其中使用以下代码更改第TH行的文本颜色:

<select>
  <option onclick="document.getElementById('tablehead').style.color=''">Default</option>
  <option onclick="document.getElementById('tablehead').style.color='red'">Red</option>
  <option onclick="document.getElementById('tablehead').style.color='yellow'">Yellow</option>
  <option onclick="document.getElementById('tablehead').style.color='green'">Green</option>
</select>

违约
红色
黄色的
绿色

这在Firefox中应该可以很好地工作,但当我在任何其他浏览器中测试它时,它都不会工作。我错过了什么

如果您正在使用jQuery,则可以侦听更改事件:

$('select').on('change', function() {
   $('#tablehead').css('color', this.value.toLowerCase());
});

您应该查看您的
选择的
onChange
事件,这需要在
选项中添加
值。您不能依赖于用户通过单击选项来选择值,正如您所看到的,并非所有浏览器都会触发单击事件

伪代码:

<select onChange="if (this.options[this.selectedIndex].value == 1) { red } else if (this.options[this.selectedIndex].value == 2) { green }">

使用本机javascript

<select onchange="document.getElementById('tablehead').style.color=this.value">
  <option value=''>Default</option>
  <option value='red'>Red</option>
  <option value='yellow'>Yellow</option>
  <option value='green'>Green</option>
</select>
<div id="tablehead">tablehead</div>

违约
红色
黄色的
绿色
桌面

演示:

虽然大家都给出了深入而好的答案,但我还是这么做了,因为我是js/jQuery新手,这对我来说最有意义:)不管怎样,它现在可以工作了!谢谢大家!