我有一个 html 表,我想将它的列转换为行。列可以包含计划文本、表单元素(输入、选择、复选框)或 html 标记(跨度、div)。考虑以下 html:
<html>
<body>
<table>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
</body>
</html>
dom操作后,我需要以下内容:
<html>
<body>
<table>
<tr>
<td>Cell 1</td>
</tr>
<tr>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
</tr>
<tr>
<td>Cell 5</td>
</tr>
<tr>
<td>Cell 6</td>
</tr>
</table>
</body>
</html>
$rows = $xpath->query('xpath to table/tr');
foreach ($rows as $row){
$cols = $xpath->query('xpath to table/tr/*');
$row->parentNode->removeChild($row);
foreach ($cols as $col){
$newNode = $this->createElement('tr');
$newNode->appendChild($col);
$node->appendChild($newNode);
}
}
echo $dom->saveHTML();
它工作正常,但它添加了空行:
<table>
<tr></tr>
<tr></tr>
<tr>
<td>Cell 1</td>
</tr>
<tr>
<td>Cell 2</td>
</tr>
<table>
知道我在做什么错吗?