JS实现搜索框文字可删除功能

时间:2021-05-26

废话不多说了,直接给大家贴js搜索框文字可删除功能,具体代码如下所示:

<!DOCTYPE html><html><head><meta charset="utf-8"><title>JS实现搜索框文字可删除</title><style>*:focus {outline: none; }body {width: 400px;margin: 100px auto;}#topsearch {height: 33px;}#topsearch .input {border: 1px solid #77c03a;height: 100%;}#topsearch .input .clear {width: 30px;height: 35px;line-height: 30px;text-align: center;padding-right: 10px;visibility: hidden;opacity: 0.8;color: gray;}#topsearch input[type=text] {height:20px;width: 250px;border: none;padding: 5px;}#topsearch div {float: left;}#topsearch button {width: 100px;height: 35px;background: #77c03a;color: #fff;border: none;}</style></head><body><div id="topsearch"><div class="input"><input type="text" id="search"><span class="clear" id="delete">×</span></div><button type="button" name="searchz">Search</button></div><script>document.getElementById("search").addEventListener("keyup", function() {if (this.value.length > 0) {document.getElementById("delete").style.visibility = "visible";document.getElementById("delete").onclick = function() {document.getElementById("search").value = "";}} else {document.getElementById("delete").style.visibility = "hidden";}});</script></body></html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章