【问题标题】:How do i change the color of <h:selectOneMenu> Dropdown menu on selecting an entry in JSF?如何在选择 JSF 中的条目时更改 <h:selectOneMenu> 下拉菜单的颜色?
【发布时间】:2016-04-08 07:07:16
【问题描述】:

这是一个示例代码:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:a4j="http://richfaces.org/a4j"
 xmlns:rich="http://richfaces.org/rich">
<script type="text/javascript" src="js/DataTable.js"></script>
<h:body>
<h:selectOneMenu value="test1"
styleClass="combobox" id="profileID"> 
<f:selectItem itemLabel="--Select--" itemValue="--Select--"/>
<f:selectItem itemLabel="Yes" itemValue="Yes"/>
<f:selectItem itemLabel="No" itemValue="No"/>
</h:selectOneMenu>
</h:body>
</ui:composition>

现在,当我选择一个选项时,我希望最小化的下拉菜单矩形更改其背景颜色,突出显示所选选项。如果我选​​择“是”,矩形应该变成“绿色”,如果我选择“否”,矩形应该变成“红色”。如何实现?

【问题讨论】:

标签: javascript html css jsf drop-down-menu


【解决方案1】:

您可以保留data-* 属性以存储所选选项的color 值。

document.getElementById('cars').addEventListener('change', function() {
  var bgColor = this.options[this.options.selectedIndex].dataset.color;
  this.style.backgroundColor = bgColor;
  [].forEach.call(this.options, function(option) {
    option.style.backgroundColor = 'white'; //As options will inherit backgroundColor from parent `select`, do set it as `#fff` after every onchange
  });
  this.options[this.options.selectedIndex].style.backgroundColor = bgColor; //If you want selected color of `data-color`
});
<select id="cars">
  <option data-color="green" value="volvo">Volvo</option>
  <option data-color="green" value="saab">Saab</option>
  <option data-color="pink" value="opel">Opel</option>
  <option data-color="blue" value="audi">Audi</option>
</select>

【讨论】:

  • 我会介意回答这个问题,因为它不是问题的一部分!请编辑您的问题并提供已解析(浏览器可读)的 HTML。
  • 你现在可以帮忙@RayonDabre 吗?
【解决方案2】:

应该这样做:

$('select').change(function(){
  if (this.value == 'volvo') {
    $(this).css('background','lime');
  }
  if (this.value == 'saab') {
    $(this).css('background','red');
  }
  if (this.value == 'opel') {
    $(this).css('background','blue');
  }
  if (this.value == 'audi') {
    $(this).css('background','yellow');
  }
});
option {
  background: #fff;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

【讨论】:

  • option 我猜应该没有颜色
  • $(this) 指的是select
  • 我什么时候说没有了! ;(
  • 选项没有任何颜色
  • 一旦设置了.css,选项从父select继承background-color..再次打开选择,你会找到它..
【解决方案3】:

做一些 jQuery 并获得所需的行为。

$("body").on('change', 'select', function(){
            var val = $(this).val();
            if(val == 'volvo'){
                $(this).css("background-color", 'red');
            }
            if(val == 'saab'){
                $(this).css("background-color", 'green');
            }
        })

【讨论】:

    【解决方案4】:

    您可以使用option:checked 突出显示选定的选项。

    :checked 伪类最初适用于具有 HTML4 选中和选中的属性

    来源:w3.org

    option:checked {
      background-color: lightgreen;
    }
    <select>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>

    【讨论】:

    • 这是选择下拉而不是复选框。
    • @DivyeshSavaliya:option:checked 甚至适用于下拉菜单。检查上面的 sn-p。
    • @检查sn-p
    • 仍然无法运行您的演示。检查 robert 代码...它正在运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多