这篇文章给大家介绍使用JavaScrip怎么实现一个搜索框功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>搜索框</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
border: 0;
}
#search{
width: 550px;
height: 35px;
margin: 100px auto;
}
#search input{
width: 492px;
height: 31px;
border: 2px solid #f10215;
outline-style: none;/* 消除原来的边框默认属性 */
float: left;
padding-left: 4px;/* 让文字在搜索的时候距离框4px */
color: #888;
}
#search button{
width: 50px;
height: 35px;
background-color: #f10215;
float: left;
color: white;
}
</style>
<script type="text/javascript">
var keyword = "iphone 11";//搜索框中默认的搜索词
window.onload = function(){
//得到按钮的对象
var btnsearch = document.getElementById("search").getElementsByTagName("button")[0];
//得到搜索框的对象
var txt = document.getElementById("search").getElementsByTagName("input")[0];
//为搜索框注册焦点事件
txt.onfocus = function(){
//当在焦点上时让搜索框文字变成黑色
txt.style.color = "black";
//如果搜索框为关键字的时候,注册焦点就让搜索框为空
if (txt.value == keyword) {
txt.value = "";
}
}
//为搜索框注册失去焦点事件
txt.onblur = function(){
//在失去焦点的时候如果搜索框内容为空,就让搜索框显示默认关键字
if (txt.value == "") {
this.value = keyword;
this.style.color = "#888";
}
}
}
</script>
</head>
<body>
<div id="search">
<input type="text" value="iphone 11" />
<button>搜索</button>
</div>
</body>
</html>
onfocus事件:事件在对象获得焦点时发生,常用在表单中
onblur事件:事件在对象失去焦点时发生
css中的属性:outline用于修饰元素的轮廓;
关于使用JavaScrip怎么实现一个搜索框功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。