`
齐晓威_518
  • 浏览: 606101 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

(转)jquery each解析数组 json xml

 
阅读更多

jquery each解析数组: 

 var arr = [ "one", "two", "three", "four"];      
$.each(arr, function(){      
alert(this);      
});      
//上面这个each输出的结果分别为:one,two,three,four     

var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]      
$.each(arr1, function(i, item){      
alert(item[0]);      
});      
//其实arr1为一个二维数组,item相当于取每一个一维数组,    
//item[0]相对于取每一个一维数组里的第一个值    
//所以上面这个each输出分别为:1   4   7      


var obj = { one:1, two:2, three:3, four:4};      
$.each(obj, function(key, val) {      
alert(obj[key]);            
});    
//这个each就有更厉害了,能循环每一个属性      
//输出结果为:1   2  3  4    

 

$(function() {
          function objInit(obj) {//下拉列表框初始化
              return $(obj).html("<option>请选择</option>");
          }
          var arrData = { //定义一个数组保存相关数据
              厂商1: { 品牌1_1: "型号1_1_1,型号1_1_2", 品牌1_2: "型号1_2_1,型号1_2_2" },
              厂商2: { 品牌2_1: "型号2_1_1,型号2_1_2", 品牌2_2: "型号2_2_1,型号2_2_2" },
              厂商3: { 品牌3_1: "型号3_1_1,型号3_1_2", 品牌3_2: "型号3_2_1,型号3_2_2" }
          };

          $.each(arrData, function(pF) { //遍历数据增加厂商项
              $("#selF").append("<option>" + pF + "</option>");
          });//厂商1 厂商2 厂商3

          $("#selF").change(function() { //厂商列表框选项改变事件
              objInit("#selT");
              objInit("#selC");

              $.each(arrData, function(pF, pS) {
                  if ($("#selF option:selected").text() == pF) { //如果厂商选中项与数据匹配

                      $.each(pS, function(pT, pC) { //遍历数据增加品牌项
                          $("#selT").append("<option>" + pT + "</option>");
                      });

                      $("#selT").change(function() { //品牌列表框选项改变事件
                          objInit("#selC");
                          $.each(pS, function(pT, pC) {

                              if ($("#selT option:selected").text() == pT) { //如果品牌选中项与数据匹配

                                  $.each(pC.split(","), function() { //遍历数据增加型号项
                                      $("#selC").append("<option>" + this + "</option>");
                                  });
                              }
                          });
                      });

                  }
              });
          });

          $("#Button1").click(function() { //注册按钮单击事件
              var strValue = "您选择的厂商:";
              strValue += $("#selF option:selected").text();
              strValue += "&nbsp;您选择的品牌:";
              strValue += $("#selT option:selected").text();
              strValue += "&nbsp;您选择的型号:";
              strValue += $("#selC option:selected").text();
              $("#divTip")
              .show()
              .addClass("clsTip")
              .html(strValue); //显示提示信息并增加样式
          });
      })

 

jquery each解析json

 

json:

var data=[
  {
    "name": "陶国荣",
    "sex": "男",
    "email": "tao_guo_rong@163.com"
  },
  {
    "name": "李建洲",
    "sex": "女",
    "email": "xiaoli@163.com"
  }
]

 

 

  $(function() {
            $("#Button1").click(function() { //按钮单击事件
                //打开文件,并通过回调函数处理获取的数据
                $.getJSON("UserInfo.json", function(data) {
                    $("#divTip").empty(); //先清空标记中的内容
                    var strHTML = ""; //初始化保存内容变量
                    $.each(data, function(InfoIndex, Info) { //遍历获取的数据
                        strHTML += "姓名:" + Info["name"] + "<br>";
                        strHTML += "性别:" + Info["sex"] + "<br>";
                        strHTML += "邮箱:" + Info["email"] + "<hr>";
                    })
                    $("#divTip").html(strHTML); //显示处理后的数据
                })
            })
        })

 

 

jquery each解析xml

 

<?xml version="1.0" encoding="utf-8" ?>
<Info>
  <User id="1">
    <name>陶国荣</name>
    <sex>男</sex>
    <email>tao_guo_rong@163.com</email>
  </User>

  <User id="2">
    <name>李建洲</name>
    <sex>女</sex>
    <email>xiaoli@163.com</email>
  </User>
</Info>

 

   $(function() {
            $("#Button1").click(function() { //按钮单击事件
                //打开文件,并通过回调函数处理获取的数据
                $.get("UserInfo.xml", function(data) {
                    $("#divTip").empty(); //先清空标记中的内容
                    var strHTML = ""; //初始化保存内容变量
                    $(data).find("User").each(function() { //遍历获取的数据
                        var $strUser = $(this);
                        strHTML += "姓名:" + $strUser.find("name").text() + "<br>";
                        strHTML += "性别:" + $strUser.find("sex").text() + "<br>";
                        strHTML += "邮箱:" + $strUser.find("email").text() + "<hr>";
                    })
                    $("#divTip").html(strHTML); //显示处理后的数据
                })
            })
        })

分享到:
评论

相关推荐

    JQuery权威指南源代码

    使用$.each()工具函数遍历数组 使用$.each()工具函数遍历ajaxSettings对象 使用$.grep()工具函数筛选数组中的元素 使用$.map()工具函数变更数组中的元素 使用$.inArray()工具函数搜索数组中指定元素的位置 使用...

    jQuery权威指南-源代码

    6.1.3 jQuery中的全局函数getJSON()/164 6.1.4 jQuery中的全局函数getScript()/166 6.1.5 jQuery中异步加载XML文档/168 6.2 请求服务器数据/170 6.2.1 $.get()请求数据/170 6.2.2 $.post()请求数据/172 6.2.3...

    jquery-1.1.3 效率提高800%

    如果键对应的值是数组,jQuery会将其值赋给同一个键属性。 例如 {foo:["bar1", "bar2"]} 变为 '&foo=bar1&foo=bar2'。 dataType( Intelligent Guess (xml or html)) 数据类型: String 期待由服务器返回值...

    jquery插件使用方法大全

     jquery提供了很多便利的函数,如each(fn),但是使用这些函数的前提是:你使用的对象是Jquery对象。使一个Dom对象成为一个Jquery对象很简单,通过下面一些方式(只是一部分): 代码 var a = $("#cid"); var b = $...

    jQuery 1.5 API 中文版

    objjQuery.each( obj, fn( index, valueOfElement ) ) objjQuery.extend( [deep,] target, obj1 [, objN] ) arrjQuery.grep( array, fn( element, index ) [, invert] ) arrjQuery.makeArray( obj ) arrjQuery.map( ...

    jQuery详细教程

    jQuery详细教程,讲解很透彻, 一. jQuery 语法实例 $(this).hide() 演示 jQuery hide() 函数,隐藏当前的 HTML 元素。 $("#test").hide() 演示 jQuery hide() 函数,隐藏 id="test" 的元素。 $("p").hide() ...

    jquery1.11.0手册

    jQuery 1.11.0 速查表 核心 jQuery 核心函数 jQuery([sel,[context]]) jQuery(html,[ownerDoc])1.8* jQuery(callback) jQuery.holdReady(hold) jQuery 对象访问 each(callback) size() length selector ...

    ajax经典怎样传输数据

    // 用Jquery处理xml数据 $(xml).find('Table').each(function() { var loginname = $(this).find("Loginname").text(); var name = $(this).find("Name").text(); $("#users").append("&lt;li&gt;" + loginname + " -...

    用模版生成HTML的的框架jquery.tmpl使用详解

    这些情况下,动态请求返回的数据一般不是已拼好的 HTML 就是 JSON 或 XML,总之不在浏览器端拼数据就在服务器端拼数据。不过,从传输量方面来看,返回 HTML 不划算,而在 web 传输方面,现在更多的是使用 JSON 而...

    Beginning.Hybrid.Mobile.Application.Development.1484213

    Role of JSON over XML in hybrid application Code Security This book will change the paradigm of mobile application development as the efforts are less, learning curve is small, success ratio is high ...

    simple-tags

    * Allow old tags field for each custom post type that use post tags. * Version 2.0-beta3 : * Fix a bug when the st_the_tags() function is called. * Version 2.0-beta2 : * Restore empty templates ...

Global site tag (gtag.js) - Google Analytics