1 line
3.8 KiB
JSON
1 line
3.8 KiB
JSON
{"name":"each","type":"method","title":".each()","deprecated":null,"removed":null,"desc":"Iterate over a jQuery object, executing a function for each matched element. ","categories":["miscellaneous/collection-manipulation","traversing","version/1.0"],"entries":[{"return":"jQuery","signatures":{"added":"1.0","argument":{"desc":"A function to execute for each matched element.","name":"function(index, Element)","type":"Function"}},"examples":[{"desc":"Iterate over three divs and sets their color property.","code":"\n$( document.body ).click(function() {\n $( \"div\" ).each(function( i ) {\n if ( this.style.color !== \"blue\" ) {\n this.style.color = \"blue\";\n } else {\n this.style.color = \"\";\n }\n });\n});\n","css":"\n div {\n color: red;\n text-align: center;\n cursor: pointer;\n font-weight: bolder;\n width: 300px;\n }\n","html":"\n<div>Click here</div>\n<div>to iterate through</div>\n<div>these divs.</div>\n"},{"desc":"To access a jQuery object instead of the regular DOM element, use <code>$( this )</code>. For example:","code":"\n$( \"span\" ).click(function() {\n $( \"li\" ).each(function() {\n $( this ).toggleClass( \"example\" );\n });\n});\n","css":"\n ul {\n font-size: 18px;\n margin: 0;\n }\n span {\n color: blue;\n text-decoration: underline;\n cursor: pointer;\n }\n .example {\n font-style: italic;\n }\n","html":"\nTo do list: <span>(click here to change)</span>\n<ul>\n <li>Eat</li>\n <li>Sleep</li>\n <li>Be merry</li>\n</ul>\n"},{"desc":"Use <code>return false</code> to break out of each() loops early.","code":"\n$( \"button\" ).click(function() {\n $( \"div\" ).each(function( index, element ) {\n // element == this\n $( element ).css( \"backgroundColor\", \"yellow\" );\n if ( $( this ).is( \"#stop\" ) ) {\n $( \"span\" ).text( \"Stopped at div index #\" + index );\n return false;\n }\n });\n});\n","css":"\n div {\n width: 40px;\n height: 40px;\n margin: 5px;\n float: left;\n border: 2px blue solid;\n text-align: center;\n }\n span {\n color: red;\n }\n","html":"\n<button>Change colors</button>\n<span></span>\n<div></div>\n<div></div>\n<div></div>\n<div></div>\n<div id=\"stop\">Stop here</div>\n<div></div>\n<div></div>\n<div></div>\n"}],"longdesc":"\n <p>The <code>.each()</code> method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword <code>this</code> refers to the element.</p>\n <p>Suppose you have a simple unordered list on the page:</p>\n <pre><code>\n<ul>\n <li>foo</li>\n <li>bar</li>\n</ul>\n </code></pre>\n <p>You can select the list items and iterate across them:</p>\n <pre><code>\n$( \"li\" ).each(function( index ) {\n console.log( index + \": \" + $( this ).text() );\n});\n </code></pre>\n <p>A message is thus logged for each item in the list:</p>\n <p>\n <samp>0: foo</samp>\n <br/>\n <samp>1: bar</samp>\n </p>\n <p>You can stop the loop from within the callback function by returning <code>false</code>.</p>\n <p>Note: most jQuery methods that return a jQuery object also loop through the set of elements in the jQuery collection — a process known as <i>implicit iteration</i>. When this occurs, it is often unnecessary to <i>explicitly</i> iterate with the <code>.each()</code> method:</p>\n <pre><code>\n// The .each() method is unnecessary here:\n$( \"li\" ).each(function() {\n $( this ).addClass( \"foo\" );\n});\n\n// Instead, you should rely on implicit iteration:\n$( \"li\" ).addClass( \"bar\" );\n </code></pre>\n "}]} |