JavaScript切换搜索引擎的导航网页搜索框实例代码


Posted in Javascript onJune 11, 2017

废话不多说了,直接给大家贴代码了,具体代码如下所述:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style>
    #search ul {
      list-style-type: none;
      display: block;
      width: 100px;
      height: 33px;
      margin: 0;
      padding: 0;
      border: 0px;
      float: left;
    }
    #search li {
      border: 0px;
      margin: 0px;
      padding: 0px;
    }
    #search .selected {
      display: block;
    }
    #search form {
      margin: 0px;
      padding: 0px;
    }
    #search input {
      height: 30px;
      width: 400px;
      margin: 0px;
    }
    #search .button {
      font-size: 17px;
      font-weight: 600;
      color: #002D96;
      height: 30px;
      width: 110px;
      margin-left: 50px;
      background: #e6efc2;
      opacity: 0.8;
      border: #7fb80e 1px solid;
      cursor: pointer;
      -webkit-border-radius: 2px;
      border-radius: 2px;
    }
  </style>
</head>
<body>
<div id="search" align="center">
  <table>
    <tr>
      <td>
        <ul>
          <li style="display:block;"><img width="80" src="https://www.baidu.com/img/bd_logo1.png"/></li>
          <li style="display:none;"><img width="80" src="https://www.google.com.hk/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png"/></li>
          <li style="display:none;"><img width="80" src="https://www.sogou.com/images/logo2014/error180x60.png"/></li>
        </ul>
      </td>
      <td id="from_box" style="padding-left:10px;">
        <form id="from_baidu" style="display:block" action="http://www.baidu.com/baidu" target="_blank" method="get">
          <input name="" type="hidden" value="baidu"/>
          <input type="text" name="word"/>
          <input class="button" type="submit" value="百度一下" onMouseOver="this.style.opacity='1'" onMouseOut="this.style.opacity='0.7'"/>
        </form>
        <form id="from_google" style="display:none" action="http://www.google.com/search" target="_blank" method="get">
          <input type="text" name="q"/>
          <input class="button" type="submit" value="google搜索" onMouseOver="this.style.opacity='1'" onMouseOut="this.style.opacity='0.7'"/>
        </form>
        <form id="from_sougou" style="display:none" action="http://www.sogou.com/web" target="_blank" name="sogou_queryform">
          <input type="text" name="query">
          <input class="button" type="submit" value="sougou搜索" onMouseOver="this.style.opacity='1'" onMouseOut="this.style.opacity='0.7'"/>
        </form>
      </td>
    </tr>
  </table>
</div>
<script>
  var search = document.getElementById("search");
  var formbox = document.getElementById('from_box');
  var forms = formbox.getElementsByTagName("form");
  var ul = search.getElementsByTagName("ul")[0];
  var li = ul.getElementsByTagName("li");
  var length = li.length;
  li[0].onclick = function() {
    for(var i = 1; i < length; i++) {
      li[i].style.display = "block";
    }
  }
  var n = 0; //第一个显示表单的位置
  for(var i = 1; i < length; i++) {
    li[i].onclick = function(a) {
      return function() {
        //交换显示的html内容
        var temp = li[0].innerHTML;
        li[0].innerHTML = this.innerHTML;
        this.innerHTML = temp;
        for(var j = 1; j < length; j++) {
          li[j].style.display ="none";
        }
        //交换表单的显示
        //alert(li[0].innerHTML.substring(37,7));
        //alert(li[0].innerHTML.indexOf('baidu'));
        hidden_from(); //隐藏表单
        if(li[0].innerHTML.indexOf('baidu') > 0) {
          document.getElementById('from_baidu').style.display = 'block';
        } else if(li[0].innerHTML.indexOf('google') > 0) {
          document.getElementById('from_google').style.display = 'block';
        } else if(li[0].innerHTML.indexOf('sougou') > 0) {
          document.getElementById('from_sougou').style.display = 'block';
        }
        //alert(this.innerHTML);
        //forms[n].style.display = "none";
        //forms[a].style.display = "block";
        //n = a;
      }
    }(i);
    li[i].onmouseover = function() {
      this.style.border ="#7fb80e 1px solid";
      this.style.background ="#f2eada";
    }
    li[i].onmouseout = function() {
      this.style.border = "0px";
      this.style.background = "inherit";
    }
  }
  //隐藏搜索框表单的函数
  function hidden_from() {
    for(var j = 0; j < forms.length; j++) {
      forms[j].style.display = "none";
    }
  }
</script>
</body>
</html>

以上所述是小编给大家介绍的JavaScript切换搜索引擎的导航网页搜索框实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Javascript 相关文章推荐
jquery 单引号和双引号的区别及使用注意
Jul 31 Javascript
解析JavaScript中点号“.”的多义性
Dec 02 Javascript
jQuery 获取/设置/删除DOM元素的属性以a元素为例
May 23 Javascript
JavaScript中诡异的delete操作符
Mar 12 Javascript
浅析jQuery Mobile的初始化事件
Dec 03 Javascript
js+ajax实现获取文件大小的方法
Dec 08 Javascript
Jquery元素追加和删除的实现方法
May 24 Javascript
jQuery simpleModal插件的使用介绍
Aug 30 Javascript
详解js中call与apply关键字的作用
Nov 21 Javascript
Bootstrap学习笔记 轮播(Carousel)插件
Mar 21 Javascript
通过学习bootstrop导航条学会修改bootstrop颜色基调
Jun 11 Javascript
JS实现select选中option触发事件操作示例
Jul 13 Javascript
jQuery复合事件结合toggle()方法的用法示例
Jun 10 #jQuery
jQuery复合事件用法示例
Jun 10 #jQuery
jQuery简单绑定单个事件的方法示例
Jun 10 #jQuery
jQuery正则验证注册页面经典实例
Jun 10 #jQuery
详解Angular4中路由Router类的跳转navigate
Jun 09 #Javascript
基于vue2.0实现的级联选择器
Jun 09 #Javascript
JavaScript设计模式之代理模式详解
Jun 09 #Javascript
You might like
再次研究下cache_lite
2007/02/14 PHP
PHP基于接口技术实现简单的多态应用完整实例
2017/04/26 PHP
PHP实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法分析
2017/05/29 PHP
php中文语义分析实现方法示例
2019/09/28 PHP
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
jQuery 操作下拉列表框实现代码
2010/02/22 Javascript
JS模块与命名空间的介绍
2013/03/22 Javascript
可自己添加html的伪弹出框实现代码
2013/09/08 Javascript
鼠标左键单击冲突的问题解决方法(防止冒泡)
2014/05/14 Javascript
json字符串之间的相互转换示例代码
2014/08/21 Javascript
js实现固定显示区域内自动缩放图片的方法
2015/07/18 Javascript
基于jQuery实现简单的折叠菜单效果
2015/11/23 Javascript
jQuery配合coin-slider插件制作幻灯片效果的流程解析
2016/05/13 Javascript
前端开发必知的15个jQuery小技巧
2017/01/22 Javascript
使用BootStrap实现标签切换原理解析
2017/03/14 Javascript
使用Node.js实现RESTful API的示例
2017/08/01 Javascript
微信小程序报错:this.setData is not a function的解决办法
2017/09/27 Javascript
通过js动态创建标签,并设置属性方法
2018/02/24 Javascript
浅谈Vue项目骨架屏注入实践
2019/08/05 Javascript
浅谈JSON5解决了JSON的两大痛点
2020/12/14 Javascript
Python学习之asyncore模块用法实例教程
2014/09/29 Python
Python编写简单的HTML页面合并脚本
2016/07/11 Python
pandas修改DataFrame列名的实现方法
2019/02/22 Python
python调用动态链接库的基本过程详解
2019/06/19 Python
Python创建临时文件和文件夹
2020/08/05 Python
Python实现http接口自动化测试的示例代码
2020/10/09 Python
英国的一家创新礼品和小工具零售商:Menkind
2019/08/24 全球购物
意大利顶级奢侈品电商:LUISAVIAROMA(支持中文)
2020/05/26 全球购物
幼儿运动会邀请函
2014/01/17 职场文书
三年级语文教学反思
2014/02/01 职场文书
高校教师岗位职责
2014/03/18 职场文书
给校长的建议书300字
2014/05/16 职场文书
2015年社区中秋节活动总结
2015/03/23 职场文书
五年级作文之劳动作文
2019/11/12 职场文书
nginx配置proxy_pass中url末尾带/与不带/的区别详解
2021/03/31 Servers
xhunter1.sys可以删除嘛? win11提示xhunter1.sys驱动不兼容解决办法
2022/09/23 数码科技