jQuery——动态给表格添加序号
$(function(){ //$(‘table tr:not(:first)‘).remove(); var len = $(‘table tr‘).length; for(var i = 1;i<len;i++){ $(‘table tr:eq(‘+i+‘) td:first‘).text(i); } });
原始代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script> <title>jQuery</title> </head> <body> <table width=500 border=2> <tr><td width=250>序号</td><td width=250>项目</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> <tr><td></td><td>Port</td></tr> </table> </body> </html>
变成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script> <title>jQuery</title> </head> <body> <table width=500 border=2> <tr><td width=250>序号</td><td width=250>项目</td></tr> <tr><td>1</td><td>Port</td></tr> <tr><td>2</td><td>Port</td></tr> <tr><td>3</td><td>Port</td></tr> <tr><td>4</td><td>Port</td></tr> <tr><td>5</td><td>Port</td></tr> <tr><td>6</td><td>Port</td></tr> <tr><td>7</td><td>Port</td></tr> <tr><td>8</td><td>Port</td></tr> </table> </body> </html>
时间: 2024-12-28 14:58:19