我想要更改在QueryPath中标识的标记本身。具体地说,我想像这样转换一个锚标签
<a href="abc.html">Example</a>
转到
<?php Blah-Blah ?>Example</a>
或
<?php Blah-Blah2 ?>
我可以找到锚标签并检索它的元素:
$qp->find('a[href]'); $href = $qp->attr('href');
但是,有没有办法在QueryPath中更改/替换标记本身呢?
或者,我也可以用<div id="specific">标签包装组件--如果可能的话,我想我可以用$qp->top('div[id="specific"]');搜索它,然后用php代码替换整个子标签(锚标签及其元素)。
然而,我在QueryPath中找不到任何一种方法...
发布于 2014-11-05 21:25:22
function renameTag( DOMElement $oldTag, $newTagName ) {
$document = $oldTag->ownerDocument;
$newTag = $document->createElement($newTagName);
$oldTag->parentNode->replaceChild($newTag, $oldTag);
foreach ($oldTag->attributes as $attribute) {
$newTag->setAttribute($attribute->name, $attribute->value);
}
foreach (iterator_to_array($oldTag->childNodes) as $child) {
$newTag->appendChild($oldTag->removeChild($child));
}
return $newTag;
}和
$qp->find('a[href]')->each(function($index,$element){
renameTag($element,'?php');
});您必须对查询中的所有元素进行迭代...
https://stackoverflow.com/questions/26198421
复制相似问题