当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JQuery查找子元素find()和遍历集合each的方法总结

JQuery查找子元素find()和遍历集合each的方法总结

2019年03月22日  | 移动技术网IT编程  | 我要评论

1.html代码

<div name="students" school="hk"> 
 <input type="boy" name="zhangsan" value="206"> 
 <input type="girl" name="lisi" value="108"> 
 </div> 

 2.jquery

<script type="text/javascript"> 
 /* find() 查找子元素方法 */ 
 var aaa = $("div[name='students'][school='hk']").find("input[type='boy'][name='zhangsan']"); 
 console.log(aaa.val()); 
  
 /* $(".child",parent); 方法查找子元素*/ 
 var bbb = $($("input[type='boy'][name='zhangsan']"),$("div[name='students'][school='hk']")); 
 console.log(aaa.val()); 
 
 /* each()方法遍历数组 */ 
 var arr = [ "one", "two", "three", "four" ]; 
 $.each(arr, function() { 
  console.log(this); 
 }); 
  
 /* each()方法处理json */ 
 var obj = { 
  one : 1, 
  two : 2, 
  three : 3, 
  four : 4 
 }; 
 $.each(obj, function(key, val) { 
  console.log(obj[key]); 
 }); 
  
 /* each()方法处理二维数组 */ 
 var arr1 = [ [ 11, 44, 33 ], [ 4, 6, 6 ], [ 7, 20, 9 ] ] 
 $.each(arr1, function(i, item) { 
  console.log(item[0]); 
 }); 
 
 /* each()方法处理html元素 */ 
 $("div[name='students'][school='hk'] > input").each(function() { 
  console.log($(this).val()); 
 }); 
 </script> 

以上这篇jquery查找子元素find()和遍历集合each的方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网