在 dom 中创建 child div
creating child div in dom
下面的代码成功帮助我创建了 div。
$dom = new DOMDocument();
$ele = $dom->createElement('div', $textcon);
$dom->appendChild($ele);
$html = $dom->saveXML();
fwrite($myfile,$html);
创建主要 div 后,我无法在下面的代码中创建 child div
$file = "http://dd/showcase.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$element = $doc->getElementsByTagName('div');
$dom = $element;
$ele = $dom->createElement('div', $textcon);
$dom->appendChild($ele);
$html = $dom->saveXML();
fwrite($myfile,$html);
方法
getElementsByTagName('div')
returns 所有名为 'div' 的元素的列表,而不是单个元素。因此,您需要将 child div 添加到上述方法返回的列表的第一个元素中。
$dom = $element[0];
这可能会解决问题
<?php
$file = "http://dd/showcase.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$ele = $doc->createElement('div', $textcon);
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-col; width :100%;');
$doc->appendChild($element);
$html = $doc->saveXML();
fwrite($myfile,$html);
?>
试试这个。
谢谢
下面的代码成功帮助我创建了 div。
$dom = new DOMDocument();
$ele = $dom->createElement('div', $textcon);
$dom->appendChild($ele);
$html = $dom->saveXML();
fwrite($myfile,$html);
创建主要 div 后,我无法在下面的代码中创建 child div
$file = "http://dd/showcase.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$element = $doc->getElementsByTagName('div');
$dom = $element;
$ele = $dom->createElement('div', $textcon);
$dom->appendChild($ele);
$html = $dom->saveXML();
fwrite($myfile,$html);
方法
getElementsByTagName('div')
returns 所有名为 'div' 的元素的列表,而不是单个元素。因此,您需要将 child div 添加到上述方法返回的列表的第一个元素中。
$dom = $element[0];
这可能会解决问题
<?php
$file = "http://dd/showcase.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$ele = $doc->createElement('div', $textcon);
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-col; width :100%;');
$doc->appendChild($element);
$html = $doc->saveXML();
fwrite($myfile,$html);
?>
试试这个。
谢谢