是否可以在<option>标签中添加<div>或<span>?

38

如何在 <option> 标签内添加 <div> 或者 <span> 标签?

我想让这一行成为 <option> ,当然它需要一个值和其他的属性,但我需要在该选项的文本旁边放一个红色的圆圈,这可行吗?

代码如下:

<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>
6个回答

25
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

"翻译成中文是:"

{{链接1:演示}}

"。

1
:after 是一个伪元素,我认为你不能将它的样式移动到 <option> 元素的内联 CSS 中。你必须使用外部 CSS。 - Zoltan Toth
什么可能会阻止 :after 呢?因为我已经将 CSS 代码添加到我的 CSS 中,但它不起作用 :( - Abude
7
非常感谢,但是你的代码在Chrome或IE>7.0上无法运行,你可以在jsfiddle上测试。 - Abude
7
2019年无法工作 - Jason Howard
我建议您编辑答案并告诉用户这个解决方案已经不再适用。 - Asons
显示剩余10条评论

23

不行。根据MDN的说法,只允许以下内容:

允许的内容:文本,有时可能需要使用转义字符(比如&eacute;


8
不行,至少不是有效的。

0
不行。要实现这个效果,您可以尝试以下方法: 使用 div 显示所选项目,并在其中放置一个 div 或按钮,就像下拉按钮一样。当单击按钮时,它会显示另一个包含所有选项的 div。
有点复杂,因此不必仅为了美观而实现该效果。

-1

你可以使用这个自定义选择输入来完成它

参考链接: 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>


-2

你可以使用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 复制外部资源: 这里是一个 fiddle

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

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

虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - Kevin
好的...我添加了必要的部分,链接不会改变,因为这是我的fiddle... :) - Arslan Tabassum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接