@ SubstanceD的解決方案是我找到的最好的解決方案,但它有一些可訪問性問題,所以我重新使用它來使用單選按鈕的字段集。
HTML:
<div id="colourlist">red</div>
<fieldset id="colours">
<label for="red">red<input type="radio" name="foo" id="red"/></label>
<label for="blue">blue <input type="radio" name="foo" id="blue"/> </label>
<label for="green">green<input type="radio" name="foo" id="green"/></label>
<label for="orange">orange<input type="radio" name="foo" id="orange"/></label>
</fieldset>
<div id="preview"></div>
CSS:
body{
margin: 0;
padding: 50px;
}
input {
opacity:0;
}
label {
display:block;
height:20px;
}
#colourlist{
position: absolute;
border: 1px solid #B5B0AC;
width: 200px;
height: 21px;
background: url('http://www.thermaxindia.com/images/dropdown_arrow.JPG') 180px 0 no-repeat;
}
label:hover{
background-color: #3399FF;
}
#colours{
display: none;
position: relative;
top: 22px;
left: 0;
width: 200px;
position: relative;
border: 1px solid #B5B0AC;
overflow-y: scroll;
height:60px;
}
#preview{
display: none;
position: relative;
background-color: #fff;
border: 1px solid #ccc;
padding: 10px;
width: 250px;
height: 30px;
}
JS:
$("#colours label").on('mouseenter', function(){
var O = $(this).offset();
var CO = $("#colours").offset();
$("#preview").css("top", O.top-150).show();
$("#preview").css("left", CO.left+160);
var text = $(this).text();
$("#preview").css("background-color", text);
});
$("#colours input").on('click', function(){
var text = $(this).attr("id");
$("#colourlist").text(text);
$("#colours").hide();
$("#preview").hide();
});
$("#colourlist").on('click', function(e){
e.stopPropagation();
$("#colours").show();
});
$("body").on('click',function(e){
$("#colours").hide();
});
這裏的小提琴:http://jsfiddle.net/MikeGodin/CJbeF/109/
,如果我沒有記錯的話,你可以欺騙其他瀏覽器要知道什麼時候的任何部分elect&options是通過用div包裝它並在那裏做鼠標懸停來懸停的,但是我不認爲你可以在沒有創建自己的假選擇式下拉菜單的情況下做到這一點。 –
看看這篇文章:http://stackoverflow.com/questions/1895476/how-to-style-a-select-dropdown-with-css-only-without-javascript它有很多的有關的缺陷信息'