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 相关文章推荐
图片完美缩放
Sep 07 Javascript
深入理解JavaScript系列(46):代码复用模式(推荐篇)详解
Mar 04 Javascript
javascript数组去重的方法汇总
Apr 14 Javascript
jQuery+CSS实现一个侧滑导航菜单代码
May 09 Javascript
JSON字符串和对象相互转换实例分析
Jun 16 Javascript
js 创建对象 经典模式全面了解
Aug 16 Javascript
原生JS实现圆环拖拽效果
Apr 07 Javascript
各种选择框jQuery的选中方法(实例讲解)
Jun 27 jQuery
详解Vue 中 extend 、component 、mixins 、extends 的区别
Dec 20 Javascript
在小程序中集成redux/immutable/thunk第三方库的方法
Aug 12 Javascript
vue-cli3配置与跨域处理方法
Aug 17 Javascript
react MPA 多页配置详解
Oct 18 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
php数组中删除元素之重新索引的方法
2014/09/16 PHP
php实现查询功能(数据访问)
2017/05/23 PHP
使Ext的Template可以解析二层的json数据的方法
2007/12/22 Javascript
改善你的jQuery的25个步骤 千倍级效率提升
2010/02/11 Javascript
关于firefox的ElementTraversal 接口 使用说明
2010/11/11 Javascript
JavaScript前端图片加载管理器imagepool使用详解
2014/12/29 Javascript
jQuery及JS实现循环中暂停的方法
2015/02/02 Javascript
JavaScript实现鼠标点击后层展开效果的方法
2015/05/13 Javascript
使用Node.js实现HTTP 206内容分片的教程
2015/06/23 Javascript
jQuery实现高亮显示网页关键词的方法
2015/08/07 Javascript
JavaScript中字面量与函数的基本使用知识
2015/10/20 Javascript
详解用vue.js和laravel实现微信授权登陆
2017/06/23 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
node中使用log4js4.x版本记录日志的方法
2019/08/20 Javascript
javascript 函数的暂停和恢复实例详解
2020/04/25 Javascript
解决Echarts 显示隐藏后宽度高度变小的问题
2020/07/19 Javascript
antd 表格列宽自适应方法以及错误处理操作
2020/10/27 Javascript
Vue多选列表组件深入详解
2021/03/02 Vue.js
[03:55]DOTA2完美大师赛选手传记——LFY.MONET
2017/11/18 DOTA
python实现批量转换文件编码(批转换编码示例)
2014/01/23 Python
Python转换HTML到Text纯文本的方法
2015/01/15 Python
python实现一次创建多级目录的方法
2015/05/15 Python
在Python中操作日期和时间之gmtime()方法的使用
2015/05/22 Python
浅谈Python的Django框架中的缓存控制
2015/07/24 Python
python制作最美应用的爬虫
2015/10/28 Python
Python编程中对文件和存储器的读写示例
2016/01/25 Python
css3新增颜色表示方式分享
2014/04/15 HTML / CSS
what is the difference between ext2 and ext3
2013/11/03 面试题
好的自荐信包括什么内容
2013/11/07 职场文书
捐款倡议书
2014/04/14 职场文书
升职演讲稿范文
2014/05/23 职场文书
关于旅游的活动方案
2014/08/15 职场文书
法律进社区活动总结
2015/05/07 职场文书
消防安全月活动总结
2015/05/08 职场文书
毛主席纪念堂观后感
2015/06/17 职场文书
新课程改革心得体会
2016/01/22 职场文书