Javascript 使用jQuery仅向父表添加表ID

Javascript 使用jQuery仅向父表添加表ID,javascript,jquery,Javascript,Jquery,我有这一页: <html> <head> <body text="#000000" marginwidth="0" marginheight="0" bgcolor="#FFFFFF" onload="" topmargin="0" leftmargin="0"> <style type="text/css"> <table width="100%" cellspacing="0" cellpadding="0" align="center"

我有这一页:

<html>
<head>
<body text="#000000" marginwidth="0" marginheight="0" bgcolor="#FFFFFF" onload="" topmargin="0" leftmargin="0">
<style type="text/css">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="page-break-after:always;">
<table width="100%" cellspacing="0" cellpadding="0" align="center">
</body>
</html>
但这会将唯一ID添加到所有表中,甚至是嵌套的表。任何帮助都将不胜感激

jQuery("body > table").each(function(count){
var count = count + 1;
jQuery(this).attr("id","table"+count+"");
});

这将选择body的直接子元素(table),我相信这对您很有用。如果我理解得很好,您可以简单地将父元素
table
元素(即
body
的直接后代)作为目标,如下所示:

$('body > table').each(function(i) {
    $(this).prop('id', 'table'+(i + 1));
});

尝试使用direct child,例如
body>table
没问题,很乐意帮助:)
$('body > table').each(function(i) {
    $(this).prop('id', 'table'+(i + 1));
});