Gio*_*lia 167 html javascript css jquery
有没有一种快速的方法来创建一个右侧有图标的输入文本元素来清除输入元素本身(如谷歌搜索框)?
我环顾四周,但我只找到了如何将图标作为输入元素的背景.是否有jQuery插件或其他东西?
我想要输入文本元素中的图标,如:
--------------------------------------------------
| X|
--------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
Rok*_*jan 315
添加type="search"到您的输入
支持是相当不错的但不会在IE <10中工作
<input type="search">Run Code Online (Sandbox Code Playgroud)
如果你需要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">×</i>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
<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 serverRun 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)
诀窍是设置一些正确的填充(我使用了18px)input并将背景图像向右推,看不见(我使用过right -10px center).
18px填充将阻止文本隐藏在图标下方(可见时).
jQ将添加显示清除图标的类x(如果input有值).
现在我们所需要的是用jQ定位带有类的输入x并检测mousemove鼠标是否在18px"x"区域内; 如果在里面,添加类onX.
单击onX该类将删除所有类,重置输入值并隐藏图标.
7x7px gif: 
Base64字符串:
data:image/gif;base64,R0lGODlhBwAHAIAAAP///5KSkiH5BAAAAAAALAAAAAAHAAcAAAIMTICmsGrIXnLxuDMLADs=
Run Code Online (Sandbox Code Playgroud)
Dav*_*mas 60
我可以建议,如果您对此仅限于符合html 5标准的浏览器,只需使用:
<input type="search" />
Run Code Online (Sandbox Code Playgroud)
不可否认,在Chromium(Ubuntu 11.04)中,这确实需要在明文图像/功能出现之前在input元素内部存在文本.
参考:
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
Shi*_* Cr 24
我在CSS中创建了一个可清除的文本框.它不需要javascript代码就可以使它工作
下面是演示链接
http://codepen.io/shidhincr/pen/ICLBD
Jos*_*ier 20
根据MDN,<input type="search" />目前所有现代浏览器都支持:
<input type="search" value="Clear this." />Run Code Online (Sandbox Code Playgroud)
但是,如果您想要在浏览器中保持一致的不同行为,那么一些轻量级的替代方案只需要JavaScript:
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>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>Run Code Online (Sandbox Code Playgroud)
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>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>Run Code Online (Sandbox Code Playgroud)
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>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>Run Code Online (Sandbox Code Playgroud)
由于没有任何解决方案真正满足我们的要求,我们想出了一个简单的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/
如果你想要像谷歌一样,那么你应该知道“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/
| 归档时间: |
|
| 查看次数: |
227035 次 |
| 最近记录: |