发布时间:2025-12-10 13:03:37 浏览次数:5
在html中,我们可以使用百度首页搜索框的代码来实现自己的网页搜索功能。以下是百度搜索框的代码:
<form id="search-form" autocomplete="off" action="https://www.baidu.com/s" target="_blank"><span class="bg s_ipt_wr"><input type="text" name="wd" id="kw" class="s_ipt" maxlength="255" autocomplete="off" autofocus></span><span class="bg s_btn_wr"><input type="submit" value="百度一下" id="su" class="s_btn"></span></form>
上述代码中,form标签表示搜索框的外层容器。form标签中有两个span标签,分别表示文本输入框和搜索按钮。文本输入框由input标签完成,name属性设置为wd,表示搜索关键字。搜索按钮也由input标签完成,type属性值为submit,表示按钮的作用是提交表单进行搜索。表单提交的地址为https://www.baidu.com/s。
为了使用户在输入关键字时有更好的体验,我们还可以设置一些搜索框的特效。例如:
<script>document.getElementById("kw").onfocus=function(){this.style.borderColor="#3385ff";document.getElementById("su").style.backgroundColor="#3385ff";}document.getElementById("kw").onblur=function(){this.style.borderColor="#e0e0e0";document.getElementById("su").style.backgroundColor="#3385ff";}</script>上述代码为搜索框设置了聚焦和失焦事件。当搜索框聚焦时,文本输入框的周围边框变为蓝色,搜索按钮的背景色变为蓝色。当搜索框失焦时,边框和背景色都还原。通过这些特效的设置,搜索框的交互体验得到了提升。