锚标记在列表项中不起作用 [英] Anchor tag not working inside List Item

查看:55
本文介绍了锚标记在列表项中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://jsfiddle.net/1exbczjy/

<body>
<section class="main">
<form class="search" action="">
    <input type="search" id ="searchit" placeholder="search.." />
    <ul class="results" id="searchlist">
    </ul>
</form>
</section>
</body>  

这是我要运行的代码的演示,我的原始代码包含js文件,该文件使用innerHTML函数动态填充ul类,但输出与我提供的伪列表数据相同.

This is a demo of the code that I am trying to run , my original code contains js file which is dynamically populating my ul class using the innerHTML function but the output is same as the dummy list data I have provided.

我无法理解为什么我的列表标签不起作用.我尝试使用此站点上提供的其他答案来解决该问题,检查z索引以及a和li标签的绝对位置和相对位置.

I am not able to understand why my list tag does not work . I have tried to resolve it using other answers provided on this site , checking for z index and absolute and relative position of a and li tag.

推荐答案

原因是因为在click上,input失去了:focus(如 shaochuancs 所指出),并且在点击锚点之前,该内容是隐藏的. click事件由mousedown mouseup事件组成.焦点丢失在页面上任何位置的mousedown上触发.因此,只需防止锚标记的mousedown上的:focus丢失.其余代码将按预期运行,因为在mouseup上触发了锚标记单击.

The reason is because on click, the input loses its :focus (as pointed out by shaochuancs) and the content is hidden before the click on the anchor is registered. A click event consists of mousedown and mouseup event. The loss of focus is triggered on mousedown anywhere on the page. So just prevent the loss of :focus on mousedown on the anchor tags. The rest of the code would function as expected, because the anchor tag click is triggered on mouseup.

因此,只需在锚标签上的mousedown上的preventDefault()即可使用一些基本的javascript/jQuery处理该问题.

So the issue can be dealt with some basic javascript/jQuery, by simply preventDefault() on mousedown on the anchor tags.

$("a").mousedown(function(ev) {
  ev.preventDefault();
}

请注意,在此代码段中,链接不被加载,因为它被框架阻塞了.但是您可以在控制台中看到单击了链接.

Note that in this code snippet the link doesn't load because it is blocked by the frame. But you can see in the console that the link is clicked.

$("a").mousedown(function(ev) {
  ev.preventDefault();
  console.log($(this).attr("href"));
  console.log("Click triggered");
});

/* * Copyright (c) 2012 Thibaut Courouble
 * Licensed under the MIT License
   ================================================== */

body {
  background: #f7f7f7;
  color: #404040;
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 13px;
  font-weight: normal;
  line-height: 20px;
}

a {
  color: #1e7ad3;
  text-decoration: none;
}

a:hover {
  text-decoration: underline
}

.container,
.main {
  width: 640px;
  margin-left: auto;
  margin-right: auto;
  height: 300px;
}

.main {
  margin-top: 50px
}

input {
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #555860;
}

.search {
  position: relative;
  margin: 0 auto;
  width: 300px;
}

.search input {
  height: 26px;
  width: 100%;
  padding: 0 12px 0 25px;
  border-width: 1px;
  border-style: solid;
  border-color: #a8acbc #babdcc #c0c3d2;
  border-radius: 13px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  -o-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -moz-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -ms-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  -o-box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
  box-shadow: inset 0 1px #e5e7ed, 0 1px 0 #fcfcfc;
}

.search input:focus {
  outline: none;
  border-color: #66b1ee;
  -webkit-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -moz-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -ms-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  -o-box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
  box-shadow: 0 0 2px rgba(85, 168, 236, 0.9);
}

.search input:focus+.results {
  display: block
}

.search .results {
  display: none;
  position: absolute;
  top: 35px;
  left: 0;
  right: 0;
  z-index: 10000;
  padding: 0;
  margin: 0;
  border-width: 1px;
  border-style: solid;
  border-color: #cbcfe2 #c8cee7 #c4c7d7;
  border-radius: 3px;
  background-color: #fdfdfd;
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fdfdfd), color-stop(100%, #eceef4));
  background-image: -webkit-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -moz-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -ms-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: -o-linear-gradient(top, #fdfdfd, #eceef4);
  background-image: linear-gradient(top, #fdfdfd, #eceef4);
  -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -ms-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.search .results li {
  display: block
}

.search .results li:first-child {
  margin-top: -1px
}

.search .results li:first-child:before,
.search .results li:first-child:after {
  display: block;
  content: '';
  width: 0;
  height: 0;
  position: absolute;
  left: 50%;
  margin-left: -5px;
  border: 5px outset transparent;
}

.search .results li:first-child:before {
  border-bottom: 5px solid #c4c7d7;
  top: -11px;
}

.search .results li:first-child:after {
  border-bottom: 5px solid #fdfdfd;
  top: -10px;
}

.search .results li:first-child:hover:before,
.search .results li:first-child:hover:after {
  display: none
}

.search .results li:last-child {
  margin-bottom: -1px
}

.search .results li a {
  display: block;
  position: relative;
  margin: 0 -1px;
  padding: 6px 40px 6px 10px;
  color: #808394;
  font-weight: 500;
  text-shadow: 0 1px #fff;
  border: 1px solid transparent;
  border-radius: 3px;
}

.search .results li a span {
  font-weight: 200
}

.search .results li a:before {
  content: '';
  width: 18px;
  height: 18px;
  position: absolute;
  top: 50%;
  right: 10px;
  margin-top: -9px;
}

.search .results li a:hover {
  text-decoration: none;
  color: #fff;
  text-shadow: 0 -1px rgba(0, 0, 0, 0.3);
  border-color: #2380dd #2179d5 #1a60aa;
  background-color: #338cdf;
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #59aaf4), color-stop(100%, #338cdf));
  background-image: -webkit-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -moz-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -ms-linear-gradient(top, #59aaf4, #338cdf);
  background-image: -o-linear-gradient(top, #59aaf4, #338cdf);
  background-image: linear-gradient(top, #59aaf4, #338cdf);
  -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -moz-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -ms-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  -o-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
}

:-moz-placeholder {
  color: #a7aabc;
  font-weight: 200;
}

::-webkit-input-placeholder {
  color: #a7aabc;
  font-weight: 200;
}

.search li {
  padding: 0px;
}

.search li a {
  margin: 0px;
  display: block;
  width: 100%;
  height: 100%;
}

.lt-ie9 .search input {
  line-height: 26px
}


/*adding effect when the mouse is hovered over list item*/


/*adding effect when the mouse is hovered over list item*/

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>Input Autocomplete Suggestions Demo</title>
<link rel="shortcut icon" href="http://designshack.net/favicon.ico">
<link rel="icon" href="http://designshack.net/favicon.ico">

<body>
  <section class="main">
    <form class="search" action="">
      <input type="search" id="searchit" placeholder="search.." />
      <ul class="results" id="searchlist">
        <li><a href="https://www.google.com">Tag A</a></li>
        <li><a href="https://www.facebook.com">Tag B</a></li>
      </ul>
    </form>
  </section>
</body>

这篇关于锚标记在列表项中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆