如何在 jQuery 中遍历数组和对象?
若要在 jQuery 中遍历数组和对象,请使用 jQuery forEach() 循环。你可以尝试运行以下代码来学习如何循环数组和对象 -
示例
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#button1").click(function(){ var arr = [{subject: "Maths", id:3}, {subject: "History", id:7}]; arr.forEach(function(el, index) { alert(el.id+" "+el.subject); }); }); }); </script> </head> <body> <button id="button1"> Result </button> </body> </html>
广告