我有以下代码:
$DOMDocument = @DOMImplementation::createDocument(null, 'html');
$width = array(200, 700);
$colgroup = $DOMDocument->createElement('colgroup');
foreach($width as $key => $width) {
$precentageWidth = round(($width*100)/array_sum($width));
$col = $DOMDocument->createElement('col');
$col->setAttribute('width', 'test');
$colgroup->appendChild($col);
}
$baseelement = $DOMDocument->documentElement;
$baseelement->appendChild($colgroup);
echo $DOMDocument->saveHTML();
预期结果:
<html><colgroup><col width="test"></col><col width="test"></col></colgroup></html>
得到结果:
<html><colgroup><col width="test"><col width="test"></colgroup></html>
所以我的问题是:</col>
标签缺失;为什么?
根据规范,</col>
结束标签禁止
这与<img>
标签类似-您永远不会写<img src="x.png"></img>
,对吗?
因为结束标记是禁止的,所以DOM实现正确地省略了它。