彼は、idに属性を設定するためにこの関数を作成しました:
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 を使用してそれらを削除することもできますが、それは醜い修正だと思います。