HTML选择悬停时的框选项吗?

HTML选择悬停时的框选项吗?,第1张

HTML选择悬停时的框选项吗?

这是一种实现方式,尽管我更喜欢一种插件方法(而
不是对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).    });

编辑以包括演示中使用的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类名分配给未单击的元素。这样,在鼠标移出时仍可以看到被单击的元素。

上面的CSS反映了最新的编辑,以使当前选定的和:hover-ed的内容li更加可见。


编辑是因为,好吧,我有点无聊。没什么好做的,所以我做了一个简单的插件:

(function($) {    $.fn.selectUl = function(){        var $origSelect = $(this);        var newId = $(this).attr('name') + '-ul';        var numOptions = $(this).children().length;        $('<ul id="' + newId + '"  />') .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();

笔记:

这不是绝对定位的,因此,如果您有多个页面select(您当然可以做到,并且可以正常运行),它确实会稍微(或很多)与页面流发生冲突,但是可以在CSS中进行修改(我可以想象,尽管我还没有尝试过。
它可能使用某些选项,可能允许使用dl元素代替current ul,这将允许对可用选项进行解释。
我怀疑它的优化程度可能比目前要高得多,但我暂时不会确定如何做到。也许可以睡一会儿,并且稍后浏览jQuery API可能会提供一些见解(尽管如果有人看到任何明显的东西,请发表评论!或者选择JS Fiddle,并且想要一个更好的词,将其分叉(尽管我显然会很感激)链接到任何后续的派生/更新)。
我不完全知道是什么牌可供选择,以我(因为在SO一切知识共享CC-BY-SA,根据杰夫·阿特伍德和页脚这个网站的,反正),但对于什么是值得我任何人都可以很高兴地接受以上内容,并且,随心所欲地使用它(如果有什么用的话,玩得开心!)。
之所以进行编辑,是因为我认为应该有一个可见的“指导”元素,
并且还要尝试解释所有工作原理。

Given the html:

<select name="sizes" id="sizes"  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>

Use the jQuery to call the plugin:

$('#sizes').selectUl();

这需要上面的选择,并创建ul带有以下标记的:

<ul id="sizes-ul" >    <li >Select a size:</li>    <li>XX-Small</li>    <li >X-Small</li>    <li >Small</li>    <li >Medium</li>    <li >Large</li>    <li >X-Large</li>    <li >XX-Large</li></ul>

使用的CSS(在演示中):

ul.plainSelect {        border: 1px solid #000;}ul.plainSelect li {        background-color: #f90;    display: list-item;}ul.plainSelect li.guidance {        border-bottom: 1px solid #000;    padding: 0.3em;    font-weight: bold;    font-size: 1.2em;    background-color: #fff;}ul.plainSelect li.unselected {        background-color: #fff;    display: none;}ul.plainSelect:hover li,ul.plainSelect:hover li.unselected {            display: list-item;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5149453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存