Home > Article > Web Front-end > How to output json to table
I have been writing front-end recently. No matter how big or small you are, please come and add some knowledge to my blog.
Not much to say, it is actually a simple js for loop
js code:
$(function(){ var str=[{name:"zy",age:111,sex:0,pass:1},{name:"zay",age:112},{name:"zby",age:113},{name:"zcy",age:114}]; obj=eval(str); var ht = ''; for(var i=0;i<obj.length;i++){/ ht = ht+'<tr>'; ht = ht + '<td>' + obj[i].name + '</td>'; ht = ht + '<td>' + obj[i].age + '</td>'; ht = ht + '<td>' + obj[i].sex + '</td>'; ht = ht + '<td>' + obj[i].pass + '</td>'; ht = ht+'</tr>'; } $('#tb').append(ht); })
html code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>表格</title> <link rel="stylesheet" href="css/table.css" /> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/table.js"></script> </head> <body> <p> <table id="tb" > <tr> <td>1</td> <td>1</td> <td>1</td> </tr> </table> </p> </body> </html>
The above is the detailed content of How to output json to table. For more information, please follow other related articles on the PHP Chinese website!