zoukankan      html  css  js  c++  java
  • 使用JQuery完成省市联动效果

    使用JQuery完成省市联动效果

    Js相关技术

    JS中的数组: ["城市"]
    new Array()
    DOM树操作:
    ​ 创建节点: document.createElement
    ​ 创建文本节点: document.createTextNode
    ​ 添加节点: appendChild

    需求分析

    ​ 在我们的注册表单中,通常我们需要知道用户的籍贯,需要一个给用选择的项,当用户选中了省份之后,列出省下面所有的城市

    技术分析

    1. 准备工作 : 城市信息的数据
    2. 添加节点 : appendChild (JS)
      a. append : 添加子元素到末尾
    $("#div1").append("<font color='red'>this is replacing text</font>")
    

    b. appendTo : 给自己找一个爹,将自己添加到别人家里

      $("#div1").prepend("<font color='red'>this is replacing text</font>")
    

    和第一个效果一样
    c. prepend : 在子元素前面添加

    $("#div1").after("<font color='red'>this is replacing text</font>")
    

    d. after : 在自己的后面添加一个兄弟

       $("<font color='red'>this is replacing text</font>").appendTo("#div1")              
    

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
        <script>
    
            $(function () {
                $("#btn1").click(function () {
                    // $("#div1").append("<font color='red'>this is replacing text</font>")
                    // $("#div1").prepend("<font color='red'>this is replacing text</font>")
                    $("#div1").after("<font color='red'>this is replacing text</font>")
                    // $("<font color='red'>this is replacing text</font>").appendTo("#div1")
                });
            });
        </script>
    </head>
    <body>
    
    <input type="button" value="click me, replace text" id="btn1">
    
    <div id="div1">this is a text that will be replaced!</div>
    
    </body>
    </html>
    
    1. 遍历的操作:
    <script>
    
          var cities = ["深圳市", "东莞市", "惠州市", "广州市"];
    
          $(cities).each(function (i, n) {
              console.log(i + "====" + n);
          })
    
          $.each(cities, function (i, n) {
              console.log(i + ">>>>" + n);
    
          })
    
      </script>
    

    步骤分析:

    1. 导入JQ的文件
    2. 文档加载事件:页面初始化
    3. 进一步确定事件: change事件
    4. 函数: 得到当前选中省份
    5. 得到城市, 遍历城市数据
    6. 将遍历出来的城市添加到城市的select中

    代码实现:

    $(function(){
    				$("#province").change(function(){
    //					alert(this.value);
    					//得到城市信息
    					var cities = provinces[this.value];
    					//清空城市select中的option
    					/*var $city = $("#city");
    					//将JQ对象转成JS对象
    					var citySelect = $city.get(0)
    					citySelect.options.length = 0;*/
    					
    					$("#city").empty();  //采用JQ的方式清空
    					//遍历城市数据
    					$(cities).each(function(i,n){
    						$("#city").append("<option>"+n+"</option>");
    					});
    				});
    			});
    
  • 相关阅读:
    502 IPO 上市
    501 Find Mode in Binary Search Tree
    500 Keyboard Row 键盘行
    498 Diagonal Traverse 对角线遍历
    Django_modelform组件
    Django_RBAC_demo2 升级版权限控制组件
    Django admin组件源码流程
    Django_rbac_demo 权限控制组件框架模型
    Django_重装系统后无法使用 sqlite 数据库报错:com.intellij.execution.ExecutionException: Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
    python_面向对象小试题
  • 原文地址:https://www.cnblogs.com/zllk/p/12841836.html
Copyright © 2011-2022 走看看