欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-23 09:22:46
废话不多说了,直接给大家贴js搜索框文字可删除功能,具体代码如下所示:

废话不多说了,直接给大家贴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>