Home Web Front-end JS Tutorial Javascript_15_DOM_select exercise

Javascript_15_DOM_select exercise

Jan 18, 2017 pm 04:55 PM

Javascript_15_DOM_select exercise

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>DOM_select练习</title>
    <style type="text/css">
    a:link,a:visited{
        color: blue;
        text-decoration: none;
    }
    a:hover{
        color: red;
    }
    table{
      color:white;
      font-weight: bold;
        border: #008FF0 dashed 1px;
    }
    table th{
        border: #008FF0 dashed 1px;
        background-color: grey;
    }
    table td{
        border: #008FF0 dashed 1px;
    }
      .div_class{
        height:50px;
        width:50px;
        float:left;
        margin-right:30px;
      }
      #div_id_text{
        clear:left;
        margin-top:20px;
      }
    </style>
  </head>
  <body>    
==============我是分割线==================
    /*
     * 需求:单击按钮添加附件,删除附件
     思路:将按钮封装到行里面,变成增加行,删除行对象
     */
    <script type="text/javascript">
      function addFile_1(){
        /*
         * <!--tr>
        <td><input type="file" /> </td>
        <td><a href="javascript:void(0)">删除附件</a></td>
        </tr-->
         * 将文件选取框定义在行对象中的单元格Td
         * 删除按钮也定义在单元格Td
         * 所以只要给表格创建新的行和单元格即可。
         */
        var oTab= document.getElementById("tab_id_1");
        var oTr= oTab.insertRow();
        var oTd_file = oTr.insertCell();
        var oTd_del = oTr.insertCell();
        oTd_file.innerHTML = "<input type=&#39;file&#39; />";
        oTd_del.innerHTML  = "<a href=&#39;javascript:void(0)&#39; onclick=&#39;deleteFile(this)&#39;>删除附件</a>";
//      oTd_del.innerHTML = "<img src=&#39;1.jpg&#39; alt=&#39;删除附件&#39; onclick=&#39;deleteFile(this)&#39; />";
      }
      function deleteFile(oA){
        //a父节点是td,td的父节点才是tr
        var oTr = oA.parentNode.parentNode;
        oTr.parentNode.removeChild(oTr);
      }
    </script>  
    <table id="tab_id_1">
      <tr>
        <td><a href="javascript:void(0)" onclick="addFile_1()">添加附件</a></td>
      </tr>
      <!--tr>
        <td><input type="file" /> </td>
        <td><a href="javascript:void(0)">删除附件</a></td>
      </tr-->
    </table>
==============我是分割线==================
    /*
     *需求: 实现二级联动菜单
     */
    <script type="text/javascript">
      function selectCharacter_3(){
        var arr_1=[&#39;林黛玉&#39;,&#39;史湘云&#39;,&#39;薛宝钗&#39;,&#39;妙玉&#39;];
        var arr_2=["诸葛亮","刘备","周瑜","孙权"];
        var arr_3=[&#39;林冲&#39;,&#39;鲁智深&#39;,&#39;武松&#39;,&#39;李逵&#39;];
        var arr_4=[&#39;唐僧&#39;,&#39;孙悟空&#39;,&#39;猪八戒&#39;,&#39;沙和尚&#39;];
        var collStory = {"选择名著":[&#39;选择人物&#39;]
        ,"红楼梦":arr_1
        ,"三国演义":arr_2
        ,"水浒传":arr_3
        ,"西游记":arr_4};
    //获取两个下拉菜单对象。 
        var oSelect_3 = document.getElementById("select_id_3");
        var oSelect_4 = document.getElementById("select_id_4");
        //获取到底选择的是哪部名著。
        var index = oSelect_3.selectedIndex;
        var name=oSelect_3.options[index].innerHTML;
        //将子菜单中的内容清空一下。
        oSelect_4.length = 0;//下面这种方法也可以
        /*
      for(var x=0;x<oSelect_4.options.length; ){
     oSelect_4.removeChild(oSelect_4.options[x]);
     }*/
        //通过键(名字)到容器去获取对应的人物数组。
        var arr = collStory[name];
        //遍历这个数组。并将这个数组的元素封装成option对象,添加到子菜单中
        for(var x=0; x<arr.length; x++){
          var oOption = document.createElement("option");
          oOption.innerHTML = arr[x];
          oSelect_4.appendChild(oOption);
         }
      }
      function selectCharacter_2(){
        //var arr_1=["诸葛亮","刘备","周瑜","孙权"];
        var collStory = {"选择名著":[&#39;选择人物&#39;]
        ,"红楼梦":[&#39;林黛玉&#39;,&#39;史湘云&#39;,&#39;薛宝钗&#39;,&#39;妙玉&#39;]
        ,"三国演义":["诸葛亮","刘备","周瑜","孙权"]
        ,"水浒传":[&#39;林冲&#39;,&#39;鲁智深&#39;,&#39;武松&#39;,&#39;李逵&#39;]
        ,"西游记":[&#39;唐僧&#39;,&#39;孙悟空&#39;,&#39;猪八戒&#39;,&#39;沙和尚&#39;]};
    //获取两个下拉菜单对象。 
        var oSelect_3 = document.getElementById("select_id_3");
        var oSelect_4 = document.getElementById("select_id_4");
        //获取到底选择的是哪部名著。
        var index = oSelect_3.selectedIndex;
        var name=oSelect_3.options[index].innerHTML;
        //将子菜单中的内容清空一下。
        //oSelect_4.length = 0;//下面这种方法也可以
      for(var x=0;x<oSelect_4.options.length; ){
     oSelect_4.removeChild(oSelect_4.options[x]);
     }
        //通过键(名字)到容器去获取对应的人物数组。
        var arr = collStory[name];
        //遍历这个数组。并将这个数组的元素封装成option对象,添加到子菜单中
        for(var x=0; x<arr.length; x++){
          var oOption = document.createElement("option");
          oOption.innerHTML = arr[x];
          oSelect_4.appendChild(oOption);
         }
      }
      function selectCharacter_1(){
        //var arr_1=["诸葛亮","刘备","周瑜","孙权"];
        var collStory = {"选择名著":[&#39;选择人物&#39;]
        ,"红楼梦":[&#39;林黛玉&#39;,&#39;史湘云&#39;,&#39;薛宝钗&#39;,&#39;妙玉&#39;]
        ,"三国演义":["诸葛亮","刘备","周瑜","孙权"]
        ,"水浒传":[&#39;林冲&#39;,&#39;鲁智深&#39;,&#39;武松&#39;,&#39;李逵&#39;]
        ,"西游记":[&#39;唐僧&#39;,&#39;孙悟空&#39;,&#39;猪八戒&#39;,&#39;沙和尚&#39;]};
        //alert(collStory);//返回[object Object]
      //alert(collStory["三国演义"]);//"诸葛亮","刘备","周瑜","孙权"
      //alert(collStory["三国演义"].length);//4
    //获取两个下拉菜单对象。 
        var oSelect_3 = document.getElementById("select_id_3");
        var oSelect_4 = document.getElementById("select_id_4");
        //获取到底选择的是哪部名著。
        var index = oSelect_3.selectedIndex;
        var name=oSelect_3.options[index].innerHTML;
        //alert(name);//三国演义
        //将子菜单中的内容清空一下。
        oSelect_4.length = 0;//下面这种方法也可以
        /*
      for(var x=0;x<oSelect_4.options.length; ){
     oSelect_4.removeChild(oSelect_4.options[x]);
     }*/
        //通过键(名字)到容器去获取对应的人物数组。
        var arr = collStory[name];
        //alert(arr==arr_1);//true
        //alert(arr);
        //遍历这个数组。并将这个数组的元素封装成option对象,添加到子菜单中
        for(var x=0; x<arr.length; x++){
          var oOption = document.createElement("option");
          oOption.innerHTML = arr[x];
          oSelect_4.appendChild(oOption);
         }
      }
    </script>
    <select id="select_id_3" onchange="selectCharacter_3()">
      <option>选择名著</option>
      <option>红楼梦</option>
      <option>三国演义</option>
      <option>水浒传</option>
      <option>西游记</option>
    </select>
    <select id="select_id_4">
      <option>选择人物</option>
    </select>
    <hr />
==============我是分割线==================
    /*
     *需求: 实现二级联动菜单
     */
<script type="text/javascript">
      function selectCity(){
        var  collCities = [[&#39;选择城市&#39;]
                  ,[&#39;海淀区&#39;,&#39;朝阳区&#39;,&#39;东城区&#39;,&#39;西城区&#39;]
                  ,[&#39;济南&#39;,&#39;青岛&#39;,&#39;烟台&#39;,&#39;威海&#39;]
                  ,[&#39;沈阳&#39;,&#39;大连&#39;,&#39;鞍山&#39;,&#39;抚顺&#39;]
                  ,[&#39;石家庄&#39;,&#39;保定&#39;,&#39;邯郸&#39;,&#39;廊坊&#39;]];
        //获取两个下拉菜单对象。 
        var oSelect_1 = document.getElementById("select_id_1");
        var oSelect_2 = document.getElementById("select_id_2");
        //获取到底选择的是哪个省。
        var index = oSelect_1.selectedIndex;
        //通过角标到容器去获取对应的城市数组。
        var arrCities = collCities[index];
        //将子菜单中的内容清空一下。
        oSelect_2.length = 0;
        //遍历这个数组。并将这个数组的元素封装成option对象,添加到子菜单中
        for(var x=0; x<arrCities.length; x++){
          var oOption = document.createElement("option");
          oOption.innerHTML = arrCities[x];
          oSelect_2.appendChild(oOption);
        }
      }
    </script>
    <select id="select_id_1" onchange="selectCity()">
      <option>选择省市</option>
      <option>北京</option>
      <option>山东</option>
      <option>辽宁</option>
      <option>河北</option>
    </select>
    
    <select id="select_id_2">
      <option>选择城市</option>
    </select>    
==============我是分割线==================
    /*
     * 需求:点击三个DIV区域,让文字分别显示相应的颜色
     */
    <script type="text/javascript">
      function changeColor_1(oDiv){
        var colorVal = oDiv.style.backgroundColor;
        document.getElementById("div_id_text").style.color = colorVal;
      }
    </script>
    <div class="div_class" id="div_id_1" style="background-color:red" onclick="changeColor_1(this)"></div>
    <div class="div_class" id="div_id_2" style="background-color:green" onclick="changeColor_1(this)"></div>
    <div class="div_class" id="div_id_3" style="background-color:blue" onclick="changeColor_1(this)"></div>
    <div id="div_id_text">
      <pre class="brush:php;toolbar:false">
1 林黛玉 可叹停机德,堪怜永絮才.玉带林中挂,金簪雪里埋. 
2 薛宝钗 可叹停机德,堪怜永絮才.玉带林中挂,金簪雪里埋. 
3 贾元春 二十年来辩是谁,榴花开处照宫闱.三春争及初春景?虎兕相逢大梦归. 
4 贾探春 才自清明志自高,生于末世运偏消.清明涕泣江边望,千里东风一梦遥. 
5 史湘云 富贵又为何?襁褓之间父母违.展眼吊斜辉,湘江水逝楚云飞. 
6 妙玉 欲洁何曾洁?云空未必空.可怜金玉质,终陷淖泥中. 
7 贾迎春 子系中山狼,得志便猖狂.金闺花柳质,一载赴黄粱. 
8 贾惜春 堪破三春景不长,缁衣顿改昔年装.可怜绣户侯门女,独卧青灯古佛旁. 
9 王熙凤 凡鸟偏从末世来,都知爱慕此生才.一从二令三人木,哭向金陵事更哀. 
10 贾巧姐 势败休云贵,家亡莫论亲.偶因济村妇,巧得遇恩人. 
11 李纨 桃李春风结子完,到头谁似一盆兰.如冰水好空相妒,枉与他人作笑谈. 
12 秦可卿 情天情海幻情深,情既相逢必主淫,漫言不肖皆出荣,造衅开端实在宁. 
==============我是分割线================== /* * 需求:选择下拉列表框,让文字分别显示相应的颜色 select对象中的集合options:获取 select 对象中 option 对象的集合。 */
Copy after login

The above is the content of Javascript_15_DOM_select exercise. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles