他我有此功能的ID设置属性:设置PHP DOM不正确的HTML时<html>,<head>,<body>标签未设置
function set_atr($id, $attribute, $value, $source){
$dom = new DomDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$source);
$xp = new DOMXPath($dom);
$element = $xp->query('//*[@id="'.$id.'"]')->item(0);
if(!$element){
return false;
}
else{
$element->setAttribute($attribute, $value);
return str_replace('<meta http-equiv="content-type" content="text/html; charset=utf-8">', '', $dom->saveHTML($dom->documentElement));
}
}
当我不使用这个编辑整个文件只是它的一个凶:
<table>
<tr>
<td><h4>login</h4></td>
<td></td>
</tr>
<tr>
<td>username:</td>
<td>password:</td>
</tr>
<tr>
<td><input id="username" type="text" placeholder="pipo" name="username" class="form" /></td>
<td><input id="password" type="password" placeholder="wachtwoord" name="password" class="form" /></td>
</tr>
<tr>
<td id="error1"></td>
<td id="error2"></td>
</tr>
<tr>
<td><input type="button" value="Login" class="btn" action="url" level="cms_user_login" form="login" /></td>
<td></td>
</tr>
</table>
它假设该文件的其余部分被遗漏,并增加了额外的HTML标签是这样的:
<html>
<head></head>
<body> <table>
<tr>
<td><h4>login</h4></td>
<td></td>
</tr>
<tr>
<td>username:</td>
<td>password:</td>
</tr>
<tr>
<td><input id="username" type="text" placeholder="pipo" name="username" class="form" /></td>
<td><input id="password" type="password" placeholder="wachtwoord" name="password" class="form" /></td>
</tr>
<tr>
<td id="error1"></td>
<td id="error2"></td>
</tr>
<tr>
<td><input type="button" value="Login" class="btn" action="url" level="cms_user_login" form="login" /></td>
<td></td>
</tr>
</table></body>
</html>
它并不直接伤害,但它看起来很丑。我以这种方式使用它的原因是因为我使用了很多Ajax,并且只发送了html的代码片段而不是整个文档。
因此,任何人都知道如何让DOM停止这样做?或者这是不可能的。我也可以使用str_replace删除它们,当我使用我的功能,但我认为这是一个丑陋的修复。
看看这个问题它会有所帮助:http://stackoverflow.com/questions/4879946/domdocument-savehtml-without-html-wrapper –
啊是的,它看起来很有趣 – botenvouwer