这是一种方法,虽然我更喜欢插件化的方法(而不是硬编码 html ul):
$('#selectUl li:not(":first")').addClass('unselected');
// Used to hide the 'unselected' elements in the ul.
$('#selectUl').hover(
function(){
// mouse-over
$(this).find('li').click(
function(){
$('.unselected').removeClass('unselected');
// removes the 'unselected' style
$(this).siblings('li').addClass('unselected');
// adds 'unselected' style to all other li elements
var index = $(this).index();
$('select option:selected').removeAttr('selected');
// deselects the previously-chosen option in the select
$('select[name=size]')
.find('option:eq(' + index + ')')
.attr('selected',true);
// assumes a 1:1 relationship between the li and option elements
});
},
function(){
// mouseout (or mouseleave, if they're different, I can't remember).
});
JS Fiddle demo.
编辑包含一个alert,展示select元素值的变化:JS Fiddle demo。
编辑以包含演示中使用的 css 和 html:
html
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<ul id="selectUl">
<li>small</li>
<li>medium</li>
<li>large</li>
</ul>
css:
select {
opacity: 0.5;
}
ul {
width: 8em;
line-height: 2em;
}
li {
display: list-item;
width: 100%;
height: 2em;
border:1px solid #ccc;
border-top-width: 0;
text-indent: 1em;
background-color: #f90;
}
li:first-child {
border-top-width: 1px;
}
li.unselected {
display: none;
background-color: #fff;
}
ul#selectUl:hover li.unselected {
background-color: #fff;
}
ul#selectUl:hover li,
ul#selectUl:hover li.unselected {
display: list-item;
}
ul#selectUl:hover li {
background-color: #fc0;
}
ul#selectUl li:hover,
ul#selectUl li.unselected:hover {
background-color: #f90;
}
这会导致菜单隐藏unselected 项目,直到ul 悬停在上方,然后显示所有选项,并使用click() 事件将unselected 类名称分配给未单击的元素;这允许单击的元素在 mouseout 时仍然可见。
上面的 CSS 反映了最近的编辑,以使当前选择的和:hover-ed、li 更加可见。
Latest JS Fiddle demo.
已编辑,因为,好吧,我有点无聊。也无事可做,所以做了一个简单的插件:
(function($) {
$.fn.selectUl = function(){
var $origSelect = $(this);
var newId = $(this).attr('name') + '-ul';
var numOptions = $(this).children().length;
$('<ul id="' + newId + '" class="plainSelect" />')
.insertAfter($(this));
for (var i = 0; i < numOptions; i++) {
var text = $(this).find('option').eq(i).text();
$('<li />').text(text).appendTo('#' + newId);
}
if ($(this).find('option:selected')) {
var selected = $(this).find('option:selected').index();
$('#' + newId)
.find('li')
.not(':eq(' + selected + ')')
.addClass('unselected');
}
$('#' + newId + ' li')
.hover(
function(){
$(this).click(
function(){
var newSelect = $(this).index();
$(this)
.parent()
.find('.unselected')
.removeClass('unselected');
$(this)
.parent()
.find('li')
.not(this)
.addClass('unselected');
$($origSelect)
.find('option:selected')
.removeAttr('selected');
$($origSelect)
.find('option:eq(' + newSelect + ')')
.attr('selected',true);
});
},
function(){
});
// assuming that you don't want the 'select' visible:
$(this).hide();
return $(this);
}
})(jQuery);
$('#sizes').selectUl();
JS Fiddle demo, including the 'plug-in'.
注意事项:
- 这不是绝对定位的,所以如果您有多个
select(您当然可以这样做,并且工作正常),它确实会稍微影响页面流(或很多),但这可以在 CSS 中修改(我想,虽然我还没有尝试过)。
- 它可能与一些选项有关,可能允许使用
dl 元素代替当前的ul,这将允许对可用选项进行解释。
- 我怀疑它可以比现在优化很多,但我不确定如何做。可能睡了一会儿,然后浏览jQuery API 可能会提供一些见解(尽管如果有人看到任何明显的东西,请发表评论!或者采取JS Fiddle 并且,如果需要更好的术语,请分叉它(虽然我会显然感谢任何后续分叉/更新的链接)。
- 我不完全确定我可以使用哪些许可选项(因为 SO 上的所有内容都是 Creative Commons cc-by-sa,根据 Jeff Atwood 和本网站的 footer,无论如何) ,但是对于它的价值,我很高兴任何人都接受上述内容,并且,好吧,用它做任何你喜欢的事情(如果它有任何用处,玩得开心!)。
已编辑,因为我觉得应该有一个可见的“指导”元素,并尝试解释它是如何运作的。
给定html:
<select name="sizes" id="sizes" class="makePlain" title="Select a size:">
<option value="xxs">XX-Small</option>
<option value="xs">X-Small</option>
<option value="s">Small</option>
<option value="m">Medium</option>
<option value="l">Large</option>
<option value="xl">X-Large</option>
<option value="xxl">XX-Large</option>
</select>
使用jQuery调用插件:
$('#sizes').selectUl();
这采用上面的选择,并创建一个带有以下标记的ul:
<ul id="sizes-ul" class="plainSelect">
<li class="guidance">Select a size:</li>
<li>XX-Small</li>
<li class="unselected">X-Small</li>
<li class="unselected">Small</li>
<li class="unselected">Medium</li>
<li class="unselected">Large</li>
<li class="unselected">X-Large</li>
<li class="unselected">XX-Large</li>
</ul>
使用的 CSS(在演示中):
ul.plainSelect {
/* targets those 'ul' elements created by the plug-in */
border: 1px solid #000;
}
ul.plainSelect li {
/* this styles the 'selected' li 'option' element */
background-color: #f90;
display: list-item;
}
ul.plainSelect li.guidance {
/* this is the 'Select a Reason' guidance/explanation from the image in the question */
border-bottom: 1px solid #000;
padding: 0.3em;
font-weight: bold;
font-size: 1.2em;
background-color: #fff;
}
ul.plainSelect li.unselected {
/* hides the 'unselected' options, and removes the background-color for contrast */
background-color: #fff;
display: none;
}
ul.plainSelect:hover li,
ul.plainSelect:hover li.unselected {
/* ensures that the 'li' elements pop-up when the list is hovered over, */
/* and act/display like 'li' elements */
display: list-item;
}
JS Fiddle demo.