我想显示一个链接列表,如下拉选择,如果可能的话,不会丢失语义。这是我尝试过的。 CSS显然现在不起作用。对于select,我在JavaScript中使用location.href
模拟了一点链接但是它失去了语义值,我猜测它是可访问性的。
没有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>
答案 0 :(得分:4)
<option>
不会使用嵌套的HTML元素。
你需要做的就是设计你的<ul> <li>
样式并让它看起来像是一个原生的下拉。
这是一个有效的例子:
答案 1 :(得分:4)
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 :(得分:4)
我认为您正在寻找类似的东西?不使用Jquery和Bootstrap解决方案
网址下拉
<强> 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 :(得分:1)
我只使用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;
}
答案 4 :(得分:0)
所有解决方案只有CSS和HOVER工作完全在移动!这说明没有悬停在移动设备上是不对的...悬停状态映射到手指点击并在每个brwoser上的每个移动操作系统上工作!通常情况下,默认行为已经成功,在某些情况下,您可以使其更适用于某些JS ......
如果你想要一个只有css和没有悬停的下拉菜单,那么另一个解决方案是用一个复选框实现的:(只需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>
答案 5 :(得分:0)
快速制作组合框而不使用ID选择器并保持HTML如上所述 链接:https://codepen.io/gabep/pen/KXJoEK
首先将CSS作为JS
拳头我为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:
//使用querySelectorAll查找任何类型的特定元素并放入列表
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;