Php DomDocument未能添加";目标="家长";到

Php DomDocument未能添加";目标="家长";到,php,jquery,html,dom,domdocument,Php,Jquery,Html,Dom,Domdocument,我有一个HTML,我正在使用DOMDocument来解析它,并为每个 谢谢。我一直想知道为什么人们会用这样的类名。。。如果您将类更改为document\u部分,则整个加载过程将更轻松。。。如果您需要有不同的部分,那么添加一个id——这将使您的PHP代码在DOMDocument中变得更简单。例如,为了不使问题复杂化,我的CSST中没有那种代码看起来多余的类。你能解释一下你想在哪些元素中添加哪些类吗?另外,请看一看您能否向我们展示获取传递给addAttributes的元素初始列表的代码?此外

我有一个HTML
,我正在使用DOMDocument来解析它,并为每个



谢谢。

我一直想知道为什么人们会用这样的类名。。。如果您将类更改为
document\u部分
,则整个加载过程将更轻松。。。如果您需要有不同的部分,那么添加一个
id
——这将使您的PHP代码在DOMDocument中变得更简单。例如,为了不使问题复杂化,我的CSST中没有那种代码看起来多余的类。你能解释一下你想在哪些元素中添加哪些类吗?另外,请看一看您能否向我们展示获取传递给addAttributes的元素初始列表的代码?此外,当您指的是DomeElement时,不应检查DOMNode。DOMDocument是DOMNodes的树。但只有DomeElement可以具有属性/属性节点。参考资料是怎么回事?但是,正如我所说,代码似乎是多余的,您应该能够通过简单地将
foreach($domDocument->getElementsByTagName('a')作为$a){$a->setAttribute('target','u parent');}实现您的目标
<html>
   <body>
        <div class="document_section1">
            <a href="/hello" target="_parent">Hi!</a>
            <a href="/hello2" target="_parent">Hi 2!</a>
        </div>
        <div class="document_section2">
            <a href="/bad">This is bad</a>
            <a href="/bad1">This is bad</a>
            <a href="/bad2">This is bad</a>
        </div>
        <div class="document_section3">
            <a href="/bye" target="_parent">Bye!</a>
        </div>
    </body>
<?php

class ... {

    public function addAttributes( &$element, &$array_attributes )
    {
        if (get_class( $element ) == "DOMNode") {
        foreach ($array_attributes as $k => $v) {
            $attribute = $this->dom->createAttribute( $k );
            $attribute->value = $v;
            $element->appendChild( $attribute );
        }
        }
        else
        if (get_class( $element ) == "DOMNodeList") {
            foreach ($element as $k => $v) {
                $this->addAttributes( $v, $array_attributes );
            }
        }
    }
}

?>