【问题标题】:DOMDocument & XPath - HTML Tag of each NodeDOMDocument & XPath - 每个节点的 HTML 标签
【发布时间】:2010-11-20 07:26:42
【问题描述】:

给定以下使用DOMDocument的PHP代码:

$inputs = $xpath->query('//input | //select | //textarea', $form);

if ($inputs->length > 0)
{
    for ($j = 0; $j < $inputs->length; $j++)
    {
        $input = $inputs->item($j);

        $input->getAttribute('name'); // Returns the Attribute
        $input->getTag(); // How can I get the input, select or textarea tag?
    }
}

如何知道每个匹配节点的标签名?

【问题讨论】:

    标签: php dom xhtml domdocument


    【解决方案1】:
    $inputs = $xpath->query('//input | //select | //textarea', $form);
    
    // no need for "if ($inputs->length > 0) - the for loop won't run if it is 0
    for ($j = 0; $j < $inputs->length; $j++)
    {
      $input = $inputs->item($j);
      echo $input->nodeName;
    }
    

    见:http://www.php.net/manual/en/class.domnode.php#domnode.props.nodename

    P.S.:除了查看文档之外,var_dump() 真的很有帮助。

    【讨论】:

    • 谢谢,我尝试了 var_dump() 并且只出现了一堆 DOMDocument 对象我也尝试了 nodeValue 但它并不完全。我一直在找这个,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2011-07-28
    • 2020-11-01
    • 1970-01-01
    • 2023-03-28
    • 2012-10-22
    • 1970-01-01
    相关资源
    最近更新 更多