简体   繁体   English

Internet Explorer中选择标记选项中的CSS伪元素

[英]CSS pseudo elements in select tag options in Internet Explorer

I'm trying to add some CSS ::before content to the <option> tags of a <select multiple> depending on if the option is selected or not. 我正在尝试::before内容::before将一些CSS ::before添加到<select multiple><option>标签,具体取决于是否选择了该选项。

The below is currently working fine in Chrome & FF, but I'm having issues in IE11. 以下目前在Chrome和FF中工作正常,但我在IE11中遇到问题。

 select > option::before { display: inline-block; width: 10em; } select > option:checked::before { content: "SELECTED: "; } select > option:not(:checked)::before { content: "excluded: " ; } 
 <select multiple> <option value="1" selected="selected">1</option> <option value="2" selected="selected">2</option> <option value="3" selected="selected">3</option> <option value="4" selected="selected">4</option> <option value="5" selected="selected">5</option> </select> 

I've read other SO posts ( 1 , 2 , 3 , 4 ) about pseudo elements in IE and most seem to point to setting some sort of position or display attribute which I've tried. 我读过其他SO帖( 1234 )约在IE伪构件和最似乎指向设置某种positiondisplay属性,我已经试过。

Is this happening as <option> tags shouldn't contain child elements? 这是否发生,因为<option>标签不应包含子元素?

First Lets get one thing straight we can't create what you desire in IE because it doesn't allow anything other than <option> , <optgroup> tags inside a <select> box. First让 我们 直截了当, 我们无法在IE中创建你想要的东西,因为它不允许<select>框内的<option><optgroup>标签以外的任何东西。

This is much of a workaround I can get using CSS only method in my opinion. 在我看来,这是我可以使用CSS only方法的一种解决方法。 If you prefer script there are plenty alternatives even select2 is one of the most used plugin for this type of customizations. 如果你喜欢脚本,那么有很多选择,即使select2是这种类型的自定义最常用的插件之一。

I Have used [type="checkbox"] for attain this result 我用[type =“checkbox”]来获得这个结果

jQuery is only being used to show the value. jQuery仅用于显示值。

 var multyVal = []; $('.multiple input[type="checkbox"]').each(function() { if ($(this).is(':checked')) { var Tval = $(this).val(); multyVal.push(Tval); } }); console.log($('select').val(), multyVal); 
 select>option::before { display: inline-block; width: 10em; } select>option:checked::before { content: "SELECTED: "; } select>option:not(:checked)::before { content: "excluded: "; } .multiple { height: 60px; display: inline-block; overflow-y: scroll; background: #d4d4d4; width: 10em; border: 1px solid #999; margin-left: 10px; } .multiple [type="checkbox"] { display: none; } .multiple label { width: 100%; float: left; font-size: 13px; text-align: right; background: #fff; } .multiple label::before { display: inline-block; float: left; font-family: arial, san-sarif; font-size: 11px; } .multiple [type="checkbox"]:checked+label::before { content: "SELECTED: "; } .multiple [type="checkbox"]:checked+label { background: #666; color: #fff; } .multiple [type="checkbox"]:not(:checked)+label::before { content: "excluded: "; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select multiple> <option value="1" selected="selected">1</option> <option value="2" selected="selected">2</option> <option value="3" selected="selected">3</option> <option value="4" >4</option> <option value="5" >5</option> </select> <div class="multiple"> <input type="checkbox" name="multiple" value="1" id="rad1" checked="checked"> <label for="rad1">1</label> <input type="checkbox" name="multiple" value="2" id="rad2" checked="checked"> <label for="rad2">2</label> <input type="checkbox" name="multiple" value="3" id="rad3" checked="checked"> <label for="rad3">3</label> <input type="checkbox" name="multiple" value="4" id="rad4"> <label for="rad4">4</label> <input type="checkbox" name="multiple" value="5" id="rad5"> <label for="rad5">5</label> </div> 

Fiddle in case you want one. 如果你想要一个小提琴

Hoping this would come handy for you guys... 希望这对你们来说很方便...

Ideally I was looking for a pure CSS solution but as per the comments and @weBer 理想情况下,我正在寻找一个纯CSS解决方案,但根据评论和@weBer

The <select> tag can only contain: <select>标记只能包含:

Zero or more <option> , <optgroup> , and script-supporting elements. 零个或多个<option><optgroup>和脚本支持元素。

Therefore I will probably end up using a JavaScript solution like the below. 因此,我可能最终会使用如下所示的JavaScript解决方案。

 document.getElementById("multiSelect").onchange = function() { var opts = this.getElementsByTagName("option"); for (var i = 0; i < opts.length; i++) { if (opts[i].selected === true) { opts[i].innerHTML = "SELECTED: " + opts[i].value; } else { opts[i].innerHTML = "excluded: " + opts[i].value; } } }; 
 select { height: 10em; width: 10em; overflow-y: hidden; } 
 <select id="multiSelect" multiple> <option value="1">excluded: 1</option> <option value="2">excluded: 2</option> <option value="3">excluded: 3</option> <option value="4">excluded: 4</option> <option value="5">excluded: 5</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM