2012-04-13 35 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()) { ... }

相關問題