How to display the list of links as a drop-down list?

I want to display a list of links, such as a dropdown, without losing semantics, if possible. Here is what I have tried. CSS is obviously not working. For choice, I emulated the link a bit with location.hrefin JavaScript, but it loses its semantic meaning and accessibility, I think.

Without jQuery and Bootstrap,

How to display the list of links as a drop-down list?

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>
Run codeHide result
+4
source share
6 answers

WAI provides some examples of an emulated list using role=listboxand role=option. To ensure better accessibility, the use of aria-activedescendantand is required aria-selected.

See List Example>

OpenAjax also has an example of a working accessible combo box.

.

,

+3

<option> HTML.

, <ul> <li> .

:

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

+4

, - : Jquery Bootstrap solution

URL

HTML

<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);
}

+3

CSS, , u

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

+1

!!! , , ... ! , JS...

css NO hover, , : ( google "css checkbox hack" )

.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>
Hide result
0

combobox HTML : https://codepen.io/gabep/pen/KXJoEK

CSS first their js

fist i UL style:

   .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;
}

make the first child your box:

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

make the last child element the bottom of the dropdown menu:

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

    }

Assign a freezing effect to the list item:

.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%;
}

now Js:

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

// use querySelectorAll to search for specific elements of any type and list

  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");

// change the value in the combo box with a value depending on

  var mainitem = document.querySelectorAll(".like-select li:first-child");


  mainitem[0].innerHTML = this.innerHTML;   

 };


 } }

window.onload = load;
0
source

Source: https://habr.com/ru/post/1687461/