3 回答

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以parentNode
像這樣簡(jiǎn)單地獲取表的:
elem.parentNode
然后它將為您提供整個(gè)表格 HTML 代碼,因?yàn)槟赶蛩母讣?jí)并獲取它的innerHTML
.
所以你的最終代碼應(yīng)該是這樣的:
var elem = document.getElementById("myTable");
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
document.getElementById("showTableCode").value = elem.parentNode.innerHTML.replaceAll("<tbody>", '').replaceAll("</tbody>", '').replace(/(\r\n|\r|\n){2,}/g, '\n');
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<html>
<head>
</head>
<body>
<center>
<table id="myTable">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
</center>
<br>
<center>
<b>Your Table Code:</b><br>
<textarea cols="50" id="showTableCode" rows="10"></textarea>
</center>
</body>
</html>
或者通過(guò)一種更簡(jiǎn)單的方法,您可以使用outerHTML
而不是來(lái)執(zhí)行此操作innerHTML
。它也提供了你想要的塊的父包裝元素。
所以整個(gè)事情將是這樣的:
var elem = document.getElementById("myTable");
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
document.getElementById("showTableCode").value = elem.outerHTML.replaceAll("<tbody>", '').replaceAll("</tbody>", '').replace(/(\r\n|\r|\n){2,}/g, '\n');
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<html>
<head>
</head>
<body>
<center>
<table id="myTable">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
</center>
<br>
<center>
<b>Your Table Code:</b><br>
<textarea cols="50" id="showTableCode" rows="10"></textarea>
</center>
</body>
</html>

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以使用outerHTML而不是innerHTML:
elem = document.getElementById("myTable");
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
document.getElementById("showTableCode").value=elem.outerHTML.replaceAll("<tbody>", '').replaceAll("</tbody>", '').replace(/(\r\n|\r|\n){2,}/g, '\n');

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
你的意思是這樣的:
var table = document.getElementById("myTable");
let area = document.getElementById("showTableCode");
// For the origins of the format function please check this SO question https://stackoverflow.com/questions/3913355/how-to-format-tidy-beautify-in-javascript/3966736
function format(html) {
var tab = ' ';
var result = '';
var indent = '';
html.split(/>\s*</).forEach(function(element) {
if (element !== "tbody") {
if (element.match(/^\/\w/)) {
indent = indent.substring(tab.length);
}
result += indent + '<' + element + '>\r\n';
if (element.match(/^<?\w[^>]*[^\/]$/)) {
indent += tab;
}
}
});
return result.substring(1, result.length - 3);
}
area.value = format(table.outerHTML);
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
<html>
<head>
</head>
<body>
<center>
<table id="myTable">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
</center>
<br>
<center><b>Your Table Code:</b><br>
<textarea cols="50" id="showTableCode" rows="10">
</textarea></center>
</body>
</html>
添加回答
舉報(bào)