允许背景图片网址可点击

时间:2017-09-16 21:37:29

标签: javascript jquery html css background-image

有没有办法让这个(搜索)图标可以点击?我希望它可以用作下面列表的展开/折叠按钮。列表是HTML,我可以隐藏和显示它。我更好奇的是如何让图标可以点击以隐藏/显示ul / li列表。如果可以,可以以某种方式应用动画。这是来自w3学校的样本,但我有自己的数据。我应该将其转换为不同类型的列表吗?网上还有一些带动画的可伸缩列表的例子。



function myFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";

        }
    }
}
&#13;
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('https://www.w3schools.com/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 0px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br><br>
<div>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
</div>

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以基于jQuery调用slideToggle函数(因为您已经在使用jQuery)

  $('#myInput').click(function(){
    $('#myUL').slideToggle('slow');
});

在此处详细了解sideToggle - http://api.jquery.com/slidetoggle/

答案 1 :(得分:1)

您可以通过添加事件监听器来使任何DOM元素可点击。在您的示例中,图标是input元素的背景图像,因此您需要将其从那里删除并使其成为与其交互的独立图像。然后你可以添加事件监听器来点击,触发一个显示/隐藏列表的功能(我改变了css以开始隐藏)。

&#13;
&#13;
var searchIco = document.getElementById("search-ico");
var myUL = document.getElementById("myUL");
searchIco.addEventListener("click", function() {
  if(myUL.style.display == 'block') {
    myUL.style.display = 'none';
    // or: myUL.removeAttribute("style");
  } else {
    myUL.style.display = 'block';
  }
});

function myFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";

        }
    }
}
&#13;
* {
  box-sizing: border-box;
}
/* New */
#search-ico {
  width: 16px;
  height: 16px;
}
/* Background removed, size changed */
#myInput {
  width: 80%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 0px;
}

#myUL {
  display: none; /* Added to start hidden */
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br><br>
<div>
<!-- Add the icon as a separate image -->
<img id="search-ico" src="https://www.w3schools.com/css/searchicon.png">
<!-- end of edited -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
</div>

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>
&#13;
&#13;
&#13;