当前位置: 代码迷 >> 综合 >> jQuery——each(callback)与each(object,[callback])
  详细解决方案

jQuery——each(callback)与each(object,[callback])

热度:40   发布时间:2024-02-04 12:25:22.0

1、each(callback)与each(object,[callback])区别:

(1)调用对象不同:前者必须使用jQuery对象调用;后者只能使用$调用;

(2)遍历对象不同:前者遍历的是jQuery对象;后者还可以遍历数组等非jQuery对象;

 

2、each(callback)

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title></title><script src="js/jquery-1.8.3.js"></script></head><body><input type="button" value="提交" onclick="test()"/><br /><div id="square" style="border: 1px solid red; width: 300px;height: 300px;background-color: green;"></div><script>function test(){$("#square").hide("slow","swing");}</script><input type="checkbox" name="hobby" value="1" checked="checked" />足球<input type="checkbox" name="hobby" value="2" />篮球<input type="checkbox" name="hobby" value="3" checked="checked" />羽毛球<script>$("[name='hobby']").each(function(){console.log(this);})</script></body>
</html>

3、each(object,[callback])

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title></title><script src="js/jquery-1.8.3.js"></script></head><body><input type="button" value="提交" onclick="test()"/><br /><div id="square" style="border: 1px solid red; width: 300px;height: 300px;background-color: green;"></div><script>function test(){$("#square").hide("slow","swing");}</script><input type="checkbox" name="hobby" value="1" checked="checked" />足球<input type="checkbox" name="hobby" value="2" />篮球<input type="checkbox" name="hobby" value="3" checked="checked" />羽毛球<script>$.each($("[name='hobby']"),function(i,value){console.log(value);});</script></body>
</html>

 

  相关解决方案