要遍历jQuery中的数组和对象,请使用jQueryforEach()
循环。您可以尝试运行以下代码以了解如何遍历数组和对象-
<!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/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>