Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ECSS在输入字段中添加清除图标_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何使用ECSS在输入字段中添加清除图标

Javascript 如何使用ECSS在输入字段中添加清除图标,javascript,jquery,html,Javascript,Jquery,Html,我想知道是否有人知道如何使用materializecss在文本输入中添加clear按钮。(插入值后,将显示清除按钮)。我试过了,但没用。感谢您的帮助您必须实现这一点,不太复杂,您可以添加一个事件监听器,因为当输入的值更改时,事件称为input,每当值更改时,您可以看到该按钮是否为空,否则将隐藏它 和按钮上的另一个eventListener,将值设置为空,并在单击时隐藏自身 materialiecss主要用于设置样式,您可以在输入中添加边距或定位按钮 const-button=document.

我想知道是否有人知道如何使用materializecss在文本输入中添加clear按钮。(插入值后,将显示清除按钮)。我试过了,但没用。感谢您的帮助

您必须实现这一点,不太复杂,您可以添加一个
事件监听器
,因为当输入的值更改时,事件称为
input
,每当值更改时,您可以看到该按钮是否为空,否则将隐藏它

和按钮上的另一个
eventListener
,将值设置为空,并在单击时隐藏自身

materialiecss
主要用于设置样式,您可以在输入中添加边距或定位按钮

const-button=document.getElementById('clear')
常量myInput=document.getElementById('myInput')
myInput.addEventListener('input',function(){
如果(this.value!=“”)button.style.opacity=1
else button.style.opacity=0
});
addEventListener('click',function(){
myInput.value=“”;
this.style.opacity=0
});
*{
框大小:边框框;
}
#我的输入{
填充:0 5px;
}
#清楚的{
不透明度:0;
浮动:对;
位置:相对位置;
顶部:-55px;
过渡:不透明度0.2s线性
}

我的输入
清楚的

您必须实现这一点,不太复杂,您可以添加一个
事件监听器
,当输入的值发生变化时,该事件称为
input
,每当值发生变化时,您可以看到该按钮是否为空,否则就隐藏它

和按钮上的另一个
eventListener
,将值设置为空,并在单击时隐藏自身

materialiecss
主要用于设置样式,您可以在输入中添加边距或定位按钮

const-button=document.getElementById('clear')
常量myInput=document.getElementById('myInput')
myInput.addEventListener('input',function(){
如果(this.value!=“”)button.style.opacity=1
else button.style.opacity=0
});
addEventListener('click',function(){
myInput.value=“”;
this.style.opacity=0
});
*{
框大小:边框框;
}
#我的输入{
填充:0 5px;
}
#清楚的{
不透明度:0;
浮动:对;
位置:相对位置;
顶部:-55px;
过渡:不透明度0.2s线性
}

我的输入
清楚的

我已经创建了一个对
物化ECSS
的扩展,因此我们不必为您希望在其上使用可清除输入字段的每个文档添加侦听器

/*
将此扩展名添加到unnified material.js文件的底部,并重新缩小以用于生产。
-或
加载materialize后添加到.js文件。
*/
(函数($){
“严格使用”;
var_默认值={};
/**
*@级
*
*/
var ClearableInputField=(函数(_Component22){
_继承(ClearableInputField,_Component22);
/**
*构造工具提示实例
*@constructor
*@param{Element}el
*@param{Object}选项
*/
函数ClearableInputField(el,选项){
_classCallCheck(这是ClearableInputField);
var(this)custom1=_possibleConstructorReturn(this,(ClearableInputField.)proto(uuu | | Object.getPrototypeOf(ClearableInputField)).call(this,ClearableInputField,el,options));
_this\u custom1.el.M\u ClearableInputField=\u this\u custom1;
_这_custom1.options=$.extend({},ClearableInputField.defaults,options);
_这是自定义1.\u setupEventHandlers();
返回本客户1;
}
_createClass(
ClearableInputField,
[
{
钥匙:“摧毁”,
/**
*拆卸组件
*/
值:函数destroy(){
这一点;
this.el.M_ClearableInputField=未定义;
},
}, 
{
键:“\u setupEventHandlers”,
值:函数_setupEventHandlers(){
this.\u handlevaluechangebind=this.\u handleValueChange.bind(this);
this.\u handleinputclickbind=this.\u handleInputClick.bind(this);
var closeIcon=此。$el.兄弟姐妹(“i”);
this.el.addEventListener(“更改”,this.\u handleValueChangeBound);
this.el.addEventListener(“键控”,this.\u handleValueChangeBound);
如果(closeIcon.length)closeIcon[0],则添加了EventListener(“单击”,此.\u handleInputClickBound);
},
},
{
密钥:“_removeventhandlers”,
值:函数_removeEventHandlers(){
var closeIcon=此。$el.兄弟姐妹(“i”);
this.el.removeEventListener(“更改”,this.\u handleValueChangeBound);
this.el.removeEventListener(“键控”,this.\u handleValueChangeBound);
如果(closeIcon.length)closeIcon[0]。请删除EventListener(“单击”,此.\u handleInputClickBound);
},
},
{
键:“_handleValueChange”,
值:函数_handleValueChange(){
if(this.$el.val()==“”| | this.$el.val()==null){
//此。$el.触发器(“模糊”);
//此.$el.兄弟姐妹(“标签”).removeClass(“活动”);
这是.$el.sibbins(“i”).css(“不透明度”,0);
}否则{
这是.$el.sibbins(“i”).css(“不透明度”,1);
}
},
/**
*手柄范围鼠标向下和Touchstart
*@param{Event}e
*/
},
{
键:“_handleInputClick”,
值:函数_handleInputClick(){
这是.$el.val(“”);
此。$el.触发器(“模糊”);
此.$el.兄弟姐妹(“标签”).removeClass(“活动”);
这是.$el.sibbins(“i”).css(“不透明度”,0);
},
/**
*手柄范围鼠标向下和Touchstart