【问题标题】:is it possible to add <div> or <span> inside an <option> tag? [duplicate]是否可以在 <option> 标签内添加 <div> 或 <span> ? [复制]
【发布时间】:2012-08-09 19:28:40
【问题描述】:

如何在&lt;option&gt; 标签内添加&lt;div&gt;&lt;span&gt; 标签?

我希望行是&lt;option&gt; 当然它有一个值和一切,但我需要在该选项中的文本旁边放一个红色圆圈,这可能吗?

代码如下:

<option value="1">text<div style='background:none repeat scroll 0 0 green;height:25px;width:25px;'></div></option>
<option value="2">text<div style='background:none repeat scroll 0 0 green;height:25px;width:25px;'></div></option>
<option value="3">text<div style='background:none repeat scroll 0 0 green;height:25px;width:25px;'></div></option>
<option value="4">text<div style='background:none repeat scroll 0 0 green;height:25px;width:25px;'></div></option>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    2019 年更新

    这个解决方案不再起作用了。

    检查了最新的 Chrome、Firefox 和 Safari。


    可以在文字后面加一个红圈-http://jsfiddle.net/V8cvQ/

    option:after {
        content: " ";
        height: 5px;
        width: 5px;
        background: #c00;
        border-radius: 5px;
        display: inline-block;
    }
    

    ...

    更新

    要有不同颜色的点

    HTML

    <select>
        <option> select </option>
        <option class="red"> one </option>
        <option class="green"> two </option>
        <option class="blue"> three </option>
    </select>
    

    CSS

    option:after {
        content: " ";
        height: 5px;
        width: 5px;
        border-radius: 5px;
        display: inline-block;
    }
    
    option.red:after { background: #c00; }
    option.green:after { background: #0c0; }
    option.blue:after { background: #00c; }
    

    DEMO

    【讨论】:

    • 感谢您的回答。正是我需要的!
    • 哇,这个人干得好
    • :after 是一个伪元素,我认为您不能将其样式移动到 &lt;option&gt; 元素的内联 CSS 中。您必须使用外部 CSS
    • @ZoltanToth 非常感谢,但是您的代码在 chrome 或 IE>7.0 上无法运行,您可以在 jsfiddle 上进行测试
    • 2019 年不工作
    【解决方案2】:

    没有。根据MDN,这是允许的:

    允许的内容:带有最终转义字符的文本(如&amp;eacute;

    【讨论】:

      【解决方案3】:

      不,不可能。或者至少无效。

      【讨论】:

        【解决方案4】:

        没有。为此,您可以尝试以下操作: 使用 div 显示所选项目,并在其中放置一个 div 或一个按钮,就像下拉按钮一样。单击按钮时,它会显示另一个包含所有选项的 div。

        有点复杂,所以没有必要仅仅为了让它看起来漂亮而达到那种效果

        【讨论】:

          【解决方案5】:

          您可以使用此自定义选择输入来完成此操作

          参考:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select

          var x, i, j, selElmnt, a, b, c;
          x = document.getElementsByClassName("custom-select");
          for (i = 0; i < x.length; i++) {
            selElmnt = x[i].getElementsByTagName("select")[0];
            a = document.createElement("DIV");
            a.setAttribute("class", "select-selected");
            a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
            a2 = document.createElement("SPAN");
            a2.setAttribute("class", "square-box");
            a2.setAttribute("style", "background-color: " + selElmnt.options[selElmnt.selectedIndex].getAttribute("COLOR"));
            a.appendChild(a2);
            x[i].appendChild(a);
            b = document.createElement("DIV");
            b.setAttribute("class", "select-items select-hide");
            for (j = 0; j < selElmnt.length; j++) {
              c = document.createElement("DIV");
              c.innerHTML = selElmnt.options[j].innerHTML;
              d = document.createElement("SPAN");
              d.setAttribute("class", "square-box");
              d.setAttribute("style", "background-color: " + selElmnt.options[j].getAttribute("COLOR"));
              c.appendChild(d);
              c.addEventListener("click", function(e) {
                var y, i, k, s, h;
                s = this.parentNode.parentNode.getElementsByTagName("select")[0];
                h = this.parentNode.previousSibling;
                for (i = 0; i < s.length; i++) {
                  if (s.options[i].innerText == this.innerText) {
                    s.selectedIndex = i;
                    h.innerHTML = this.innerHTML;
                    y = this.parentNode.getElementsByClassName("same-as-selected");
                    for (k = 0; k < y.length; k++) {
                      y[k].removeAttribute("class");
                    }
                    this.setAttribute("class", "same-as-selected");
                    break;
                  }
                }
                h.click();
              });
              b.appendChild(c);
            }
            x[i].appendChild(b);
            a.addEventListener("click", function(e) {
              e.stopPropagation();
              closeAllSelect(this);
              this.nextSibling.classList.toggle("select-hide");
              this.classList.toggle("select-arrow-active");
            });
          }
          
          function closeAllSelect(elmnt) {
            var x, y, i, arrNo = [];
            x = document.getElementsByClassName("select-items");
            y = document.getElementsByClassName("select-selected");
            for (i = 0; i < y.length; i++) {
              if (elmnt == y[i]) {
                arrNo.push(i)
              } else {
                y[i].classList.remove("select-arrow-active");
              }
            }
            for (i = 0; i < x.length; i++) {
              if (arrNo.indexOf(i)) {
                x[i].classList.add("select-hide");
              }
            }
          }
          document.addEventListener("click", closeAllSelect);
          .custom-select {
            position: relative;
            font-family: Arial;
          }
          
          .custom-select select {
            display: none;
          }
          
          .select-selected {
            background-color: DodgerBlue;
          }
          
          .select-selected:after {
            position: absolute;
            content: "";
            top: 14px;
            right: 10px;
            width: 0;
            height: 0;
            border: 6px solid transparent;
            border-color: #fff transparent transparent transparent;
          }
          
          .select-selected.select-arrow-active:after {
            border-color: transparent transparent #fff transparent;
            top: 7px;
          }
          
          .select-items div,
          .select-selected {
            color: #ffffff;
            padding: 8px 16px;
            border: 1px solid transparent;
            border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
            cursor: pointer;
            user-select: none;
          }
          
          .select-items {
            position: absolute;
            background-color: DodgerBlue;
            top: 100%;
            left: 0;
            right: 0;
            z-index: 99;
          }
          
          .select-hide {
            display: none;
          }
          
          .select-items div:hover,
          .same-as-selected {
            background-color: rgba(0, 0, 0, 0.1);
          }
          
          .select-items .square-box {
            height: 18px;
            width: 18px;
            float: right;
          }
          
          .select-selected .square-box {
            height: 18px;
            width: 18px;
            right: 30px;
            position: absolute;
          }
          <div class="custom-select" style="width:200px;">
            <select>
              <option value="1" color="red">Audi</option>
              <option value="2" color="green" selected>BMW</option>
              <option value="3" color="yellow">Citroen</option>
              <option value="4">Ford</option>
              <option value="5">Honda</option>
              <option value="6">Jaguar</option>
              <option value="7">Land Rover</option>
              <option value="8">Mercedes</option>
              <option value="9">Mini</option>
              <option value="10">Nissan</option>
              <option value="11">Toyota</option>
              <option value="12">Volvo</option>
            </select>
          </div>

          【讨论】:

            【解决方案6】:

            你可以用 JS 插件替换你的选项:

            HTML:

            <h3>multiSelect = false</h3>
            <div id="combo1" class="combo"></div><br>
            &nbsp;&nbsp;<button id="get1">get1</button>&nbsp;
            <button id="set1">set1</button>
            

            JS:

            $(function(){
            var dd = [];
            for(var i=1;i<=4; i++)
                dd.push({ code: i + '', name: 'Employee ' + i });
            var cfg = {
                keyField: 'code',
                displayField: 'name',
                multiSelect: false,
                width: 200,
                boxWidth: 200,
                cols : [{
                    field: 'code', width: '30%'
                },{
                    field: 'name', width: '70%'
                }],
                data: dd
            };
            var cfg1 = $.extend({}, cfg);
            var cb1 = $('#combo1').mac('combo', cfg1);
            $('#get1').click(function(){
                alert(cb1.selected);
            });
            $('#set1').click(function(){
                cb1.select(2);
            });
            

            });

            不要忘记从 jsfiddle 复制外部资源: 这是一个小提琴

            http://jsfiddle.net/arslantabassum/p5s4jzez/

            1. copy html
            2. copy javascript
            3. copy external sources
            

            【讨论】:

            • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
            • 好的...我添加了重要部分,并且链接不会改变,因为这是我的小提琴... :)
            猜你喜欢
            • 2018-12-06
            • 2014-10-01
            • 2012-11-03
            • 1970-01-01
            • 1970-01-01
            • 2015-02-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多