像克里斯在他的评论中提到的请尝试使用DOMNode::appendChild,这将让你一个子元素添加到您选择的元素和DOMDocument::createElement实际创建的元素,像这样:
$html = file_get_contents('http://example.com');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
//get the element you want to append to
$descBox = $doc->getElementById('element1');
//create the element to append to #element1
$appended = $doc->createElement('div', 'This is a test element.');
//actually append the element
$descBox->appendChild($appended);
或者,如果你已经有了一个要追加可以create a document fragment像这样的HTML字符串:
$html = file_get_contents('http://example.com');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
//get the element you want to append to
$descBox = $doc->getElementById('element1');
//create the fragment
$fragment = $doc->createDocumentFragment();
//add content to fragment
$fragment->appendXML('<div>This is a test element.</div>');
//actually append the element
$descBox->appendChild($fragment);
请注意,使用JavaScript添加的所有元素都将无法访问到PHP。
你尝试,http://php.net/manual/en/domnode.appendchild.php?不知道'$ html'是什么,或者你想添加如此难以举例。 – chris85