Javascript 是否可以在 <option> 标签内添加 <div> 或 <span> ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11890597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is it possible to add <div> or <span> inside an <option> tag?
提问by Abude
how can i add <div>
or a <span>
tag inside an <option>
tag?
如何<div>
在<span>
标签内添加或添加<option>
标签?
I want the row to be <option>
of course it has a value and everything, but I need to put a circle red color next to the text in that option, is that possible?
我<option>
当然希望该行具有值和所有内容,但是我需要在该选项中的文本旁边放置一个红色圆圈,这可能吗?
code like:
代码如:
<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>
采纳答案by Zoltan Toth
2019 UPDATE
2019年更新
This solution doesn't work anymore.
此解决方案不再有效。
Checked in latest Chrome, Firefox and Safari.
检查了最新的 Chrome、Firefox 和 Safari。
It is possible to put a red circle after the text - http://jsfiddle.net/V8cvQ/
可以在文字后放一个红圈 - http://jsfiddle.net/V8cvQ/
option:after {
content: " ";
height: 5px;
width: 5px;
background: #c00;
border-radius: 5px;
display: inline-block;
}
...
...
UPDATE
更新
To have different color dots
要有不同颜色的点
HTML
HTML
<select>
<option> select </option>
<option class="red"> one </option>
<option class="green"> two </option>
<option class="blue"> three </option>
</select>
CSS
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; }
回答by Dave
回答by j08691
Nope, not possible. Or not valid at least.
不,不可能。或者至少无效。
回答by NSF
No. To achieve that you can try this: Use a div to show the selected item and put a div or a button inside it like a drop-down button. When the button is clicked it shows another div containing all your options.
不。要实现这一点,您可以尝试以下操作:使用 div 显示所选项目,并在其中放置一个 div 或按钮(如下拉按钮)。单击按钮时,它会显示另一个包含所有选项的 div。
A little bit complex so not necessary to achieve that effect just to make it look pretty
有点复杂所以没有必要为了让它看起来漂亮而达到那个效果
回答by Arslan Tabassum
You can replace your Options with JS plugin:
你可以用 JS 插件替换你的选项:
HTML:
HTML:
<h3>multiSelect = false</h3>
<div id="combo1" class="combo"></div><br>
<button id="get1">get1</button>
<button id="set1">set1</button>
JS:
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);
});
});
});
Don't forget to copy external sources from jsfiddle: Here is a fiddle
不要忘记从 jsfiddle 复制外部源:这是一个小提琴
http://jsfiddle.net/arslantabassum/p5s4jzez/
http://jsfiddle.net/arslantabassum/p5s4jzez/
1. copy html
2. copy javascript
3. copy external sources
回答by Ahmed Essawy
You can do it using this custom select input
您可以使用此自定义选择输入来完成
Reference: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select
参考: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>