1

使用以下 PHP 代码DOMDocument

$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?
    }
}

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

4

1 回答 1

3
$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

PS:除了查看文档之外,avar_dump()真的很有帮助。

于 2009-08-17T16:28:50.560 回答