更改HTML选项选择为图标样式

时间:2018-08-11 01:29:36

标签: javascript jquery html css css3

是否可以使选择选项看起来像图标?

我的意思是,当您单击图标时,将显示选择菜单,而不是框样式。有可能吗?

我的意思是这样

https://image.ibb.co/gBY31U/testset.png

2 个答案:

答案 0 :(得分:1)

function dropdowntoggle() {
    document.getElementById("myDropdown").classList.toggle("show");
}

window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdown = document.getElementsByClassName("dropdown-content")[0];
    if (dropdown.classList.contains('show')) {
      dropdown.classList.remove('show');
    }
  }
}

window.onload = function() {
    var selections = document.getElementsByClassName('selection');
    for(var i = 0; i < selections.length; i++) {
        var selection = selections[i];
        selection.onclick = function() {
            console.log(this.innerText);
        }
    }
}
.dropbtn {
    background-color: #3498DB;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #2980B9;
}

.without-button {
    width: 15px;
    background-color: transparent;
    color: black;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f1f1f1;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="dropdown">
<button onclick="dropdowntoggle()" class="dropbtn"><i class="fa fa-bars" aria-hidden="true"></i></button>
  <div id="myDropdown" class="dropdown-content">
    <a class="selection" href="#home">Home</a>
    <a class="selection" href="#about">About</a>
    <a class="selection" href="#contact">Contact</a>
  </div>
</div>

<!-- uncomment this to see same behaviour without button -->
<!-- <div onclick="dropdowntoggle()" class="dropbtn without-button">
   <i class="fa fa-bars" aria-hidden="true"></i>
</div> -->

答案 1 :(得分:0)

您只需在选择的下拉列表中添加超棒的字体图标(如文本!)。您只需要CSS中的一些东西,FontAwesome CSS和unicode。