简体   繁体   English

选择时更改选项的颜色

[英]Change the color of an option when selected

I want an option to change color when selected by a user.我想要一个在用户选择时更改颜色的选项。 For Example: a user selects the red option then a function would run that would change the color red.例如:用户选择红色选项,然后运行将更改红色的函数。 If the user then selected green then it would change green.如果用户随后选择了绿色,那么它将变为绿色。 etc.等等。

<select onchange="changeColor();" class="color" id="rgb">
  <option id="red" value="Red">Red</option>
  <option id="green" value="Green">Green</option>
  <option id="blue" value="Blue">Blue</option>
</select>

I started with the function below but I'm not sure where I went wrong.我从下面的函数开始,但我不确定我哪里出错了。

function changeColor() {
    var red = document.getElementById('red');
    var green = document.getElementById('green');
    var blue = document.getElementById('blue');

    if(event.target.value == red) {
      red.style.color = "red";
    } else if(event.target.value == green) {
      green.style.color = "green";
    } else if(event.target.value == blue) {
      blue.style.color = "blue";
    } else  {
      alert("There was an error!");
      }
  };

It looks like you haven't added an 'event' parameter to your function.看起来您尚未向函数添加“事件”参数。 Try this:尝试这个:

function changeColor(event) {
  var red = document.getElementById(red);
  var green = document.getElementById(green);
  var blue = document.getElementById(blue);

  if (event.target.value == red) {
    red.style.color = "red";
  } else if (event.target.value == green) {
    green.style.color = "green";
  } else if (event.target.value == blue) {
    blue.style.color = "blue";
  } else {
    alert("There was an error!");
  }};

Try this.尝试这个。 When you select an option you will recieve the <select> element in colorParam .当您选择一个选项时,您将收到colorParam<select>元素。 If you select the first option, you will get Red in colorParam.value , but you are using IDs in lowerCase, so you can use toLowerCase() function to convert it.如果您选择第一个选项,您将在colorParam.value获得Red ,但您使用的是colorParam.value形式的 ID,因此您可以使用toLowerCase()函数对其进行转换。 Then select the option element and apply styles.然后选择option元素并应用样式。

 function changeColor(colorParam) { let color = colorParam.value.toLowerCase(); var optionElement = document.getElementById(color); optionElement.style.color = color; };
 <select onchange="changeColor(this);" class="color" id="rgb"> <option id="red" value="Red">Red</option> <option id="green" value="Green">Green</option> <option id="blue" value="Blue">Blue</option> <option id="white" value="White">White</option> <option id="pink" value="Pink">Pink</option> </select>

I'm not sure you can style an <option> element.我不确定您是否可以设置<option>元素的样式。 You can style the <select> element though.不过,您可以设置<select>元素的样式。 However, color doesn't seem to be a property you can change, background-color is.但是, color似乎不是您可以更改的属性, background-color是。

If that's all you need you could as well inline the javascript code in the onchange handler attribute.如果这就是您所需要的,您还可以在onchange处理程序属性中内联 javascript 代码。 You would need to set the values of the <option> elements to valid css colours.您需要将<option>元素的值设置为有效的 css 颜色。

 <select onchange="this.style.backgroundColor=this.value"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select>

Please check onchange only selected value color will get changed.

 $(function() { //The color of the selected value changed jQuery("#state-list").on("change", function() { if (jQuery(this).val()) { let colorselect= jQuery(this).find("option:selected").attr('id'); jQuery('.statetext option').css("color", "#919ca1"); jQuery('.statetext').find("option:selected").css("color", colorselect); jQuery('.statetext').css("color", colorselect) } else { jQuery('.statetext').css("color", "#919ca1"); jQuery('.statetext option').css("color", "#919ca1"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select id="state-list" name="State" data-name="State" class="statetext" style="color: rgb(145, 156, 161);"> <option value="">State</option> <option value="CA" id="red">CA</option> <option value="BC" id="yellow">BC</option> <option value="DE" id="green">DE</option></select>

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

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