jquery - each()
2015-12-04 11:23
204 查看
.each(function)
functionType: Function( Integer index, Element element )
A function to execute for each matched element.
[b]exmple
[/b]
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>each demo</title> <style> div { width: 40px; height: 40px; margin: 5px; float: left; border: 2px blue solid; text-align: center; } span { color: red; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <button>Change colors</button> <span></span> <div></div> <div></div> <div></div> <div></div> <div id="stop">Stop here</div> <div></div> <div></div> <div></div> <script> $( "button" ).click(function() { $( "div" ).each(function( index, element ) { // element == this $( element ).css( "backgroundColor", "yellow" ); if ( $( this ).is( "#stop" ) ) { $( "span" ).text( "Stopped at div index #" + index ); return false; } }); }); </script> </body> </html>
jQuery.each()
jQuery.each( array, callback )
arrayType: Array
The array to iterate over.
callback
Type: Function( Integer indexInArray, Object value )
The function that will be executed on every object.
jQuery.each( object, callback )
objectType: Object
The object to iterate over.
callback
Type: Function( String propertyName, Object valueOfProperty )
The function that will be executed on every object.
Examples
Iterates over items in an array, accessing both the current item and its index. $.each( [ "a", "b", "c" ], function( i, l ){ alert( "Index #" + i + ": " + l ); }); Iterates over the properties in an object, accessing both the current item and its key. $.each({ name: "John", lang: "JS" }, function( k, v ) { alert( "Key: " + k + ", Value: " + v ); });
以上。
在一些使用迭代方法生成html文档的地方,使用用each函数会事半功倍。
项目代码:(仿PHP官网搜索框部分代码)
$('#search_doc').on("keyup",function(){ //remove and add the title ,make the title show before tt-dataset $('div.tt-menu h3').each(function(){ var $dataset = $(this); var dataset_clo = $dataset.closest('.tt-dataset'); // locate the tag h3 if(dataset_clo.prev().is('h3')){ dataset_clo.prev().remove(); }else if($dataset.next().text() == ''){ $dataset.remove(); } // if the category has content,then insert the title before the tt-dataset if(dataset_clo.has('a').length){ $dataset.insertBefore(dataset_clo); } }); });
相关文章推荐
- JQuery多媒体插件jQuery Media Plugin使用详解
- jQuery实现ajax调用WCF服务的方法(附带demo下载)
- jquery中的this与$(this)
- jquery.validate ie8 验证提交问题
- jQuery旋转木马式幻灯片轮播特效
- jquery.js 库中的 选择器 的 事件的绑定
- jQuery中cookie插件用法实例分析
- jQuery中this与$(this)的区别
- 常用的jquery
- jQuery中this与$(this)的区别
- JSON.stringify与jQuery.parseJSON
- 利用jQuery及AJAX技术定时更新GridView的某一列数据
- JQuery选择器大全
- 学习--jquery 自定义插件 可扩展 表单验证
- jquery获取、改变元素属性值
- 学习--jquery 自定义插件 面向对象 -- 模拟 jquery EasyUi的 自定义 alert + confirm
- jquery解决插件冲突
- jQuery-1.9.1源码分析系列(十六)ajax——ajax框架
- jQuery-1.9.1源码分析系列(十六)ajax——ajax框架
- jquery遍历表格 每一行 值