2012-04-13 68 views
1

如何检查DOM文档是否有项目如何检查DOM文档是否有项目

代码:

$iframe = $dom->getElementsByTagName('iframe'); 


foreach ($iframe as $if) { 

    //how to check whether $if has items for 
    // $if->item(0) so that I can access it without errors? 
} 
+0

'$ if-> item(0)'或'$ iframe-> item(0)'?请参阅[DOMNodeList](http://php.net/manual/en/class.domnodelist.php)。 – hakre 2012-04-13 00:29:29

回答

1

测试此节点有子节点与hasChildNodes()

foreach ($iframe as $if) { 
    if ($if->hasChildNodes()) { 
     // first child is $if->childNodes->item(0) 
    } 
} 
0

您可以检查使用像

if($if -> hasChildNodes()) { ... }