代码之家  ›  专栏  ›  技术社区  ›  Walle Cyril

如何将链接列表显示为下拉选择?

  •  5
  • Walle Cyril  · 技术社区  · 7 年前

    location.href 在JavaScript中,但它失去了语义价值和可访问性。

    没有jQuery和Bootstrap,

    如何将链接列表显示为下拉选择?

    document.getElementById("0").addEventListener("change", function (event) {
      location.href = event.target.value;
    });
    .like-select {
      appearance: select;
    }
    <p>Semantic wanted</p>
    <ul class="like-select">
      <li><a href="https://en.wikipedia.org/wiki/Main_Page">Wikipedia</a></li>
      <li><a href="https://stackoverflow.com">Stack Overflow</a></li>
      <li><a href="http://www.echojs.com/">Echo Js</a></li>
    </ul>
    
    <p>Look and feel wanted especially on mobile</p>
    <select id="0">
      <option value="https://en.wikipedia.org/wiki/Main_Page">Wikipedia</option>
      <option value="https://stackoverflow.com">Stack Overflow</option>
      <option value="http://www.echojs.com/">Echo Js</option>
    </select>
    6 回复  |  直到 7 年前
        1
  •  5
  •   allenski    7 年前

    WAI提供了多个使用 role=listbox role=option . 这需要使用 aria-activedescendant aria-selected 以获得更好的可访问性支持。

    See Examples under section: 3.13 Listbox

    对于样式,可以复制 style used by the user agent stylesheet.

    尽管如此,将链接列表样式设置为下拉选择可能是一个坏主意,因为它可能导致 unpredictable change of context

        2
  •  5
  •   Liaqat Saeed    7 年前

    我想你在找这样的东西?不使用Jquery和Bootstrap解决方案

    Url下拉列表

    <div class="dropdown">
      Select URL...
      <div class="dropdown-content">
       <ul class="like-select">
      <li><a href="https://en.wikipedia.org/wiki/Main_Page">Wikipedia</a></li>
      <li><a href="https://stackoverflow.com">Stack Overflow</a></li>
      <li><a href="http://www.echojs.com/">Echo Js</a></li>
    </ul>
      </div>
    </div>
    

    CSS

    .dropdown {
        position: relative;
        display: inline-block;
            padding: 10px;
           width:160px;
    
        border: 1px solid;
    }
    .dropdown:after{
      content: '\25BC';
      position: relative;
        font-size:14px;
       float:right;
    
    
    }
    
    .dropdown-content {
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        width: inherit;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
       top: 39px;
        left: 0;
        width: 100%;
        z-index: 1;
    }
    li a{
      text-decoration:none;
        color: black;
        padding:10px;
    }
    ul{
      padding:0;
      margin:0;
    }
    li{
          list-style: none;
        padding:10px;
    
      border-bottom:1px solid black;
    }
    li:hover{
      background-color:gray;
    
    }
    li:hover a{
        color:white;
    }
    

    JS公司

    var dropdown = document.getElementsByClassName("dropdown");
    var attribute;
    var myFunction = function() {
    attribute = this.getAttribute("data-target");
        var x = document.getElementById(attribute);
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    
    };
    for (var i = 0; i < dropdown.length; i++) {
        dropdown[i].addEventListener('click', myFunction, false);
    }
    

    Working Fiddle

        3
  •  4
  •   Matthew Barbara    7 年前

    <option> 不接受嵌套的HTML元素。

    你要做的是你的风格 <ul> <li> 让它看起来和感觉像一个本地下拉菜单。

    下面是一个工作示例:

    https://codepen.io/anon/pen/boxKRz

        4
  •  1
  •   Oscar Zhang    7 年前

    我只使用CSS制作了这个示例,希望这能帮助你

    HTML:

    <ul>
        <li id="box">Hover Me
            <ul>
              <li class="dropdown_item"><a href="http://thinkio.ca">111</a></li>
              <li class="dropdown_item"><a href="http://thinkio.ca">222</a></li>
            </ul>
        </li>
    </ul>
    

    CSS:

    ul, li {
        list-style-type: none;
        margin: 0;
        padding:0;
        height:30px;
        line-height: 30px;
        width: 100px;
    }
    #box {
        border: 1px solid #bbb;
        display: inline-block;
        cursor:default;
    }
    ul li ul {
        display: none;
        position: absolute;
        top: 40px; /* change this value based on your browser */
        left: 10px;
    }
    ul li:hover>ul:last-child {
        display: block;
        width: 100px;
    }
    ul li ul li:hover {
        background-color:rgb(33,144,255);
        color:white;
        border: 1px solid #bbb;
    }
    

    https://codepen.io/zsydyc/pen/VMGGPv

        5
  •  1
  •   ToTaTaRi    7 年前

    所有的解决方案,只要CSS和悬停,都在手机上运行良好!!!关于手机上没有悬停的评论是不对的。。。悬停状态映射到手指点击,并在每个浏览器的每个移动操作系统上工作!通常情况下,默认行为已经做到了这一点,在某些情况下,您可以使其更适用于一些JS。。。

    .checkhack {
      display: none;
    }
    
    #menu {
      display: none;
    }
    
    #menutoggle:checked + #menu {
      display: block;
    }
    <label for="menutoggle" class="checklabel">OPEN MENU</label>
    <input id="menutoggle" class="checkhack" type="checkbox" />
    <ul id="menu">
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 2</a></li>
      <li><a href="#">Link 3</a></li>
    </ul>
        6
  •  0
  •   Gabriel    7 年前

    快速创建组合框的方法,无需使用ID选择器并保持HTML如上所述 链接: https://codepen.io/gabep/pen/KXJoEK

    首先是CSS和JS

    fist I style the UL:

       .like-select {
      height:21px;
      overflow:hidden;
      width:8%;
    }
    
    .like-select li{
      appearance: select;
    color:red;
        border-left: 1px solid blue;
       border-right: 1px solid blue;
        list-style-type: none;
    }
    

    让第一个孩子成为你的盒子:

    .like-select li:first-child {
        border: 1px solid blue;
    }
    

    将最后一个子项设置为下拉列表的底部:

     .like-select li:last-child {
            border-bottom: 1px solid blue;
    
        }
    

    为列表项设置悬停效果:

    .like-select li a:hover { 
      background-color: green !important;
    }
    
    .like-select li:first-child a:hover{ 
      background-color: none !important;
    }
    
    a {
      color:blue;
      text-decoration:none;
      width:100%;
    }
    

    现在Js:

        function load() {
      //add the main item to your list you need to have it in your drop-down: 
    

    //使用queryselectoral查找任何类型的特定元素,并将其放入列表中

      var addfirst= document.querySelectorAll(".like-select li:first-child");
        var ullist = document.querySelectorAll(".like-select"); 
       ullist[0].innerHTML =  addfirst[0].outerHTML + ullist[0].innerHTML ;
    
    
       y = document.querySelectorAll(".like-select li");
    
      // do an onlick here instead of mouse over
       y[0].onmouseenter = function(){
    
         //resize wrapper event - im not going to do a toggle because you get the idea 
    var comboboxwrapper = document.querySelectorAll(".like-select");
    comboboxwrapper[0].style.height = "100px";
     }
    
    
    // loop though all other items except first-child
    var i;
        for (i = 1; i < y.length; i++) { 
     y[i].onmouseover = function(){
    
       var selecteditem =document.querySelectorAll(".like-select li");
    

    //将值悬停在组合框中,更改其中的值

      var mainitem = document.querySelectorAll(".like-select li:first-child");
    
    
      mainitem[0].innerHTML = this.innerHTML;   
    
     };
    
    
     } }
    
    window.onload = load;