我正在做我自己的搜索框,代码如下:
document.querySelector('.header-search input').addEventListener('keydown', function() {
if (this.value === "") {
document.querySelector('.header-search-reset').classList.remove('reset');
} else {
document.querySelector('.header-search-reset').classList.add('reset');
}
})
document.querySelector('.header-search-reset').addEventListener('click', function() {
document.querySelector('.header-search input').value = "";
this.classList.remove('reset');
})
input {
border: 1px solid #000;
}
.header-search input::-ms-clear {
display: none;
}
.header-search-reset {
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: opacity .2s, visibility .2s;
}
.header-search-reset.reset {
opacity: 1;
visibility: visible;
}
<div class="header-search">
<input type="text" placeholder="Search">
<button type="button" class="header-search-reset">X</button>
</div>
而且这段代码运行得很好,但是为什么只有在第二次点击键盘后才会出现该按钮。
POPMUISE
翻翻过去那场雪
红颜莎娜
相关分类