清除输入文本内的图标

Gio*_*lia 167 html javascript css jquery

有没有一种快速的方法来创建一个右侧有图标的输入文本元素来清除输入元素本身(如谷歌搜索框)?

我环顾四周,但我只找到了如何将图标作为输入元素的背景.是否有jQuery插件或其他东西?

我想要输入文本元素中的图标,如:

--------------------------------------------------
|                                               X|
--------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Rok*_*jan 315

添加type="search"到您的输入
支持是相当不错的但不会在IE <10中工作


旧浏览器的可清除输入

如果你需要IE9支持这里有一些解决方法

使用标准<input type="text">和一些HTML元素:

/**
 * Clearable text inputs
 */
$(".clearable").each(function() {
  
  var $inp = $(this).find("input:text"),
      $cle = $(this).find(".clearable__clear");

  $inp.on("input", function(){
    $cle.toggle(!!this.value);
  });
  
  $cle.on("touchstart click", function(e) {
    e.preventDefault();
    $inp.val("").trigger("input");
  });
  
});
Run Code Online (Sandbox Code Playgroud)
/* Clearable text inputs */
.clearable{
  position: relative;
  display: inline-block;
}
.clearable input[type=text]{
  padding-right: 24px;
  width: 100%;
  box-sizing: border-box;
}
.clearable__clear{
  display: none;
  position: absolute;
  right:0; top:0;
  padding: 0 8px;
  font-style: normal;
  font-size: 1.2em;
  user-select: none;
  cursor: pointer;
}
.clearable input::-ms-clear {  /* Remove IE default X */
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<span class="clearable">
  <input type="text" name="" value="" placeholder="">
  <i class="clearable__clear">&times;</i>
</span>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

仅使用a <input class="clearable" type="text">(无其他元素)

清除输入元素内的图标

设置class="clearable"并播放它的背景图片:

/**
 * Clearable text inputs
 */
function tog(v){return v?'addClass':'removeClass';} 
$(document).on('input', '.clearable', function(){
    $(this)[tog(this.value)]('x');
}).on('mousemove', '.x', function( e ){
    $(this)[tog(this.offsetWidth-18 < e.clientX-this.getBoundingClientRect().left)]('onX');
}).on('touchstart click', '.onX', function( ev ){
    ev.preventDefault();
    $(this).removeClass('x onX').val('').change();
});

// $('.clearable').trigger("input");
// Uncomment the line above if you pre-fill values from LS or server
Run Code Online (Sandbox Code Playgroud)
/* Clearable text inputs */
.clearable{
  background: #fff url(http://i.stack.imgur.com/mJotv.gif) no-repeat right -10px center;
  border: 1px solid #999;
  padding: 3px 18px 3px 4px;     /* Use the same right padding (18) in jQ! */
  border-radius: 3px;
  transition: background 0.4s;
}
.clearable.x  { background-position: right 5px center; } /* (jQ) Show icon */
.clearable.onX{ cursor: pointer; }              /* (jQ) hover cursor style */
.clearable::-ms-clear {display: none; width:0; height:0;} /* Remove IE default X */
Run Code Online (Sandbox Code Playgroud)
<input class="clearable" type="text" name="" value="" placeholder="" />


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

jsBin演示

诀窍是设置一些正确的填充(我使用了18px)input并将背景图像向右推,看不见(我使用过right -10px center).
18px填充将阻止文本隐藏在图标下方(可见时).
jQ将添加显示清除图标的类x(如果input有值).
现在我们所需要的是用jQ定位带有类的输入x并检测mousemove鼠标是否在18px"x"区域内; 如果在里面,添加类onX.
单击onX该类将删除所有类,重置输入值并隐藏图标.


7x7px gif: 清除图标7x7

Base64字符串:

data:image/gif;base64,R0lGODlhBwAHAIAAAP///5KSkiH5BAAAAAAALAAAAAAHAAcAAAIMTICmsGrIXnLxuDMLADs=
Run Code Online (Sandbox Code Playgroud)

  • 我冒昧地借用了这段代码中的一些想法来制作一个标签云生成器(带有纯css标签和upvote和downvote按钮).我希望你的浏览器看起来不错; 在http://jsfiddle.net/7PnKS/查看 (5认同)

Dav*_*mas 60

我可以建议,如果您对此仅限于符合html 5标准的浏览器,只需使用:

<input type="search" />
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示

不可否认,在Chromium(Ubuntu 11.04)中,这确实需要在明文图像/功能出现之前input元素内部存在文本.

参考:

  • 对其他开发人员的帮助:如果你使用bootstrap CSS平台删除样式"-webkit-appearance:textfield;" 在bootstrap.css输入中[type ="search"] {-webkit-box-sizing:content-box; -moz-box-sizing:content-box; box-sizing:content-box; -webkit-appearance:textfield; } (11认同)
  • 不幸的是,Firefox 不支持此功能,请参阅:https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-search-cancel-button (4认同)
  • @RiccardoBassilichi 有没有办法扩展我的 css 文件,这样我就不必编辑原始的 bootstrap.css 并且仍然可以正常工作? (2认同)

KOG*_*OGI 26

您可以使用带图像的重置按钮...

<form action="" method="get">
   <input type="text" name="search" required="required" placeholder="type here" />
   <input type="reset" value="" alt="clear" />
</form>

<style>
   input[type="text"]
   {
      height: 38px;
      font-size: 15pt;
   }

   input[type="text"]:invalid + input[type="reset"]{
     display: none;
   } 

   input[type="reset"]
   {
      background-image: url( http://png-5.findicons.com/files/icons/1150/tango/32/edit_clear.png );
      background-position: center center;
      background-repeat: no-repeat;
      height: 38px;
      width: 38px;
      border: none;
      background-color: transparent;
      cursor: pointer;
      position: relative;
      top: -9px;
      left: -44px;
   }
</style>
Run Code Online (Sandbox Code Playgroud)

请在此处查看:http://jsbin.com/uloli3/63

  • 这会重置整个表单,而不仅仅是一个字段 (9认同)

Shi*_* Cr 24

我在CSS中创建了一个可清除的文本框.它不需要javascript代码就可以使它工作

下面是演示链接

http://codepen.io/shidhincr/pen/ICLBD

  • 文本未被重新定位以为"x"按钮腾出空间.您无法看到按钮后面的文字. (3认同)

Jos*_*ier 20

根据MDN,<input type="search" />目前所有现代浏览器都支持:

输入类型=搜索

<input type="search" value="Clear this." />
Run Code Online (Sandbox Code Playgroud)


但是,如果您想要在浏览器中保持一致的不同行为,那么一些轻量级的替代方案只需要JavaScript:

选项1 - 始终显示'x':( 此处示例)

Array.prototype.forEach.call(document.querySelectorAll('.clearable-input>[data-clear-input]'), function(el) {
  el.addEventListener('click', function(e) {
    e.target.previousElementSibling.value = '';
  });
});
Run Code Online (Sandbox Code Playgroud)
.clearable-input {
  position: relative;
  display: inline-block;
}
.clearable-input > input {
  padding-right: 1.4em;
}
.clearable-input > [data-clear-input] {
  position: absolute;
  top: 0;
  right: 0;
  font-weight: bold;
  font-size: 1.4em;
  padding: 0 0.2em;
  line-height: 1em;
  cursor: pointer;
}
.clearable-input > input::-ms-clear {
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<p>Always display the 'x':</p>

<div class="clearable-input">
  <input type="text" />
  <span data-clear-input>&times;</span>
</div>

<div class="clearable-input">
  <input type="text" value="Clear this." />
  <span data-clear-input>&times;</span>
</div>
Run Code Online (Sandbox Code Playgroud)

选项2 - 仅在鼠标悬停在字段上时显示"x":( 此处示例)

Array.prototype.forEach.call(document.querySelectorAll('.clearable-input>[data-clear-input]'), function(el) {
  el.addEventListener('click', function(e) {
    e.target.previousElementSibling.value = '';
  });
});
Run Code Online (Sandbox Code Playgroud)
.clearable-input {
  position: relative;
  display: inline-block;
}
.clearable-input > input {
  padding-right: 1.4em;
}
.clearable-input:hover > [data-clear-input] {
  display: block;
}
.clearable-input > [data-clear-input] {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  font-weight: bold;
  font-size: 1.4em;
  padding: 0 0.2em;
  line-height: 1em;
  cursor: pointer;
}
.clearable-input > input::-ms-clear {
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<p>Only display the 'x' when hovering over the field:</p>

<div class="clearable-input">
  <input type="text" />
  <span data-clear-input>&times;</span>
</div>

<div class="clearable-input">
  <input type="text" value="Clear this." />
  <span data-clear-input>&times;</span>
</div>
Run Code Online (Sandbox Code Playgroud)

选项3 - 如果input元素具有值,则仅显示"x" :( 此处示例)

Array.prototype.forEach.call(document.querySelectorAll('.clearable-input'), function(el) {
  var input = el.querySelector('input');

  conditionallyHideClearIcon();
  input.addEventListener('input', conditionallyHideClearIcon);
  el.querySelector('[data-clear-input]').addEventListener('click', function(e) {
    input.value = '';
    conditionallyHideClearIcon();
  });

  function conditionallyHideClearIcon(e) {
    var target = (e && e.target) || input;
    target.nextElementSibling.style.display = target.value ? 'block' : 'none';
  }
});
Run Code Online (Sandbox Code Playgroud)
.clearable-input {
  position: relative;
  display: inline-block;
}
.clearable-input > input {
  padding-right: 1.4em;
}
.clearable-input >[data-clear-input] {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  font-weight: bold;
  font-size: 1.4em;
  padding: 0 0.2em;
  line-height: 1em;
  cursor: pointer;
}
.clearable-input > input::-ms-clear {
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<p>Only display the 'x' if the `input` element has a value:</p>

<div class="clearable-input">
  <input type="text" />
  <span data-clear-input>&times;</span>
</div>

<div class="clearable-input">
  <input type="text" value="Clear this." />
  <span data-clear-input>&times;</span>
</div>
Run Code Online (Sandbox Code Playgroud)


wld*_*nfr 9

由于没有任何解决方案真正满足我们的要求,我们想出了一个简单的jQuery插件,名为jQuery-ClearSearch -

使用它就像:

<input class="clearable" type="text" placeholder="search">

<script type="text/javascript">
    $('.clearable').clearSearch();
</script>
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/wldaunfr/FERw3/

  • 似乎无法在最新的 Google Chrome 中运行(v.70 ~ 2018 年 11 月) (2认同)

Jon*_*ski 5

如果你想要像谷歌一样,那么你应该知道“X”实际上并不在里面<input>——它们彼此相邻,外部容器的样式看起来像文本框。

HTML:

<form>
    <span class="x-input">
        <input type="text" class="x-input-text" />
        <input type="reset" />
    </span>
</form>
Run Code Online (Sandbox Code Playgroud)

CSS:

.x-input {
    border: 1px solid #ccc;
}

.x-input input.x-input-text {
    border: 0;
    outline: 0;
}
Run Code Online (Sandbox Code Playgroud)

示例: http: //jsfiddle.net/VTvNX/


小智 5

在设计模式中将文本框类型更改为“搜索”或

<input type="search">
Run Code Online (Sandbox Code Playgroud)