【问题标题】:Styling is not Working on option of datalist样式不适用于 datalist 选项
【发布时间】:2021-08-05 11:47:32
【问题描述】:

我正在尝试更改悬停选项的背景颜色,但我无法在 datalist 中执行此操作。有什么办法可以做到这一点?我尝试通过事件(onmouseoveronmouseenteronmouseleave)提供内联 css、外部 css

/*  React Js Code */
<input  type="text" list="browsers" />

<datalist id="browsers">
/* iterating values from array and showing in option */
<option style={{backgroundColor:'red'}} onMouseOver={e=>e.target.style.color = 'green'}  onMouseEnter={(e)=>e.target.style.background = 'red'} 
onMouseLeave={(e)=>e.target.style.background = 'black'} >{d}</option>
</datalist>

【问题讨论】:

    标签: javascript html css reactjs dropdown


    【解决方案1】:

    为什么不给它添加一个类并使用 css :hover 方法,例如:

    <option className="my-option" />
    

    然后在css中

    .my-option:hover{
      background-color:'red';
    }
    

    【讨论】:

    • 它不工作,在你的编辑器中运行这段代码(这是示例代码,只是为了检查工作)pastebin.com/yfY3c3DX
    • 哦,我现在明白了……你为什么不使用普通选择而不是使用 datalist 输入?
    • 由于记录数量多,我需要在下拉列表中包含搜索功能,所以我得到的最简单的方法是实现 datalist
    【解决方案2】:

    您可以使用 CSS 自定义数据列表,但您必须禁用/忽略 OOB 数据列表功能并使用 JavaScript 重写该功能,结果如下:

    input.onfocus = function() {
      browsers.style.display = 'block';
      input.style.borderRadius = "5px 5px 0 0";
    };
    for (let option of browsers.options) {
      option.onclick = function() {
        input.value = option.value;
        browsers.style.display = 'none';
        input.style.borderRadius = "5px";
      }
    };
    
    input.oninput = function() {
      currentFocus = -1;
      var text = input.value.toUpperCase();
      for (let option of browsers.options) {
        if (option.value.toUpperCase().indexOf(text) > -1) {
          option.style.display = "block";
        } else {
          option.style.display = "none";
        }
      };
    }
    var currentFocus = -1;
    input.onkeydown = function(e) {
      if (e.keyCode == 40) {
        currentFocus++
        addActive(browsers.options);
      } else if (e.keyCode == 38) {
        currentFocus--
        addActive(browsers.options);
      } else if (e.keyCode == 13) {
        e.preventDefault();
        if (currentFocus > -1) {
          /*and simulate a click on the "active" item:*/
          if (browsers.options) browsers.options[currentFocus].click();
        }
      }
    }
    
    function addActive(x) {
      if (!x) return false;
      removeActive(x);
      if (currentFocus >= x.length) currentFocus = 0;
      if (currentFocus < 0) currentFocus = (x.length - 1);
      x[currentFocus].classList.add("active");
    }
    
    function removeActive(x) {
      for (var i = 0; i < x.length; i++) {
        x[i].classList.remove("active");
      }
    }
    input {
      font-size: 18px;
      padding: 5px;
      height: 35px;
      width: 350px;
      border: 1px solid #000;
      outline: none;
      border-radius: 5px;
      color: #000;
      /*   border-bottom: none; */
    }
    
    datalist {
      position: absolute;
      background-color: white;
      border: 1px solid #000;
      border-radius: 0 0 5px 5px;
      border-top: none;
      width: 350px;
      padding: 5px;
    }
    
    option {
      background-color: white;
      padding: 4px;
      color: #000;
      margin-bottom: 1px;
      font-size: 18px;
      cursor: pointer;
      background: #eeeeee
    }
    
    option:hover,
    .active {
      background-color: red;
    }
    <input autocomplete="off" role="combobox" list="" id="input" name="browsers">
    <!-- Its important that you keep list attribute empty to hide the default dropdown icon and the browser's default datalist -->
    
    <datalist id="browsers" role="listbox">
      <option value="Chrome">Chrome</option>
      <option value="Safari">Safari</option>
      <option value="Microsoft Edge">Microsoft Edge</option>
    </datalist>

    来自DEV

    【讨论】:

    • 它不工作,在你的编辑器中运行这个代码(这是示例代码,只是为了检查工作)pastebin.com/yfY3c3DX
    • 很抱歉,我编辑了您可以看到的问题
    • 但是你可以把它转换成react,JS对我来说更容易
    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多