DOM 和 jQuery对象相互转换 以及for-in和$.each遍历方法
2016-07-04 11:59
971 查看
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="../reset/reset.css"> <script type="text/javascript" src="../jquery/jquery.min.js"></script> </head> <body> <!-- DOM对象,即是我们用传统的方法(javascript)获得的对象,jQuery对象即是用jQuery类库的选择器获得的对象; 复制代码 代码如下: var domObj = document.getElementById("id"); //DOM对象 var $obj = $("#id"); //jQuery对象; --> <div id="c"></div> <h2>锄禾日当午</h2> <h2>汗滴禾下土</h2> <h2>谁知盘中餐</h2> <h2>粒粒皆辛苦</h2> <script> var c = document.getElementById('c'); var cc = $("#c"); document.write('***************************') var domc = $('#c')[0]; var domc = $('#c').get(0); //上面两种方法都行 将jquery对象转换为dom对象 var $jqc = $(c)//将dom对象转换为jquery对象 console.log('-----------------------------------------') //遍历 var h = $('h2'); $('h2').each(function(index,value){ console.log(index); console.log(value) }); $.each(h,function(index,value){ console.log(index); console.log(value); }); var hh = {'姓名' : '张三',年龄 : 22,sex : 'nan'}; console.log(hh.sex) console.log(hh['姓名']+hh['年龄']); $.each(hh,function(index,value){ console.log(index); console.log(value); }) for(index in hh){ console.log(index); console.log(hh[index]); } var hhh = [{'姓名':'Tom'},{'age':14},123,'Happy NewYear!']; $.each(hhh,function(index,value){ console.log(index); console.log(value) }) for(index in hhh){ console.log(index); console.log(hhh[index]) } console.log('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); for( i in h){ console.log(i) } var DH = document.getElementsByTagName('h2'); for(var i=0;i<DH.length;i++){ console.log(DH[i].innerHTML) } var $dh = $(DH); console.log(DH); console.log($dh) $.each($dh,function(index,value){ console.log(index); console.log(value) }) //个人总结 如果是自己var 的对象 或者数组 用for-in 或者$.each或者$().each都可以遍历 //如果是通过DOM或者jquery获取的对象 要用$.each或者$().each遍历 //DOM对象也可以用for循环遍历 </script> </body> </html>
相关文章推荐
- php+json+ajax+jquery小例子
- jQuery文字提示与图片提示效果实现方法
- jQuery 2.0.3 源码分析 Deferrred概念
- jQuery实现的导航下拉菜单效果
- jQuery实现简单倒计时功能的方法
- jquery设置表单元素为不可用的简单代码
- jQuery实现订单提交页发送短信功能前端处理方法
- 实用jquery操作表单元素的简单代码
- jQuery实现左侧导航模块的显示与隐藏效果
- jquery事件有关问题!对象 click和document click冲突有关问题
- jQuery解析与处理服务器端返回xml格式数据的方法详解
- Jquery filter()方法简介
- Jquery中css()方法获取边框长度
- 使用jquery的hide方法实现div隐藏动画
- jQuery ajaxSubmit 实现ajax提交表单局部刷新
- jQuery通用的全局遍历方法$.each()用法实例
- 【深入浅出jQuery】源码浅析--奇技淫巧
- 【深入浅出jQuery】源码浅析--整体架构
- 十条jQuery代码片段助力Web开发效率提升
- JQuery EasyUI Combobox联动