使用getElementById可以实现相同的方法 [英] Is it possible to achieve same using getElementById

查看:131
本文介绍了使用getElementById可以实现相同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以使用DOM的getElementsByTagName获取一个html表的元素,它可以使用。

With the code below I can get elements of an html table using DOM's getElementsByTagName and it works.

$dom = new DOMDocument();
$dom->loadHTMLFile("any.html");

## get table by tagName

$tables= $dom->getElementsByTagName('table');
$table = $tables->item(0);

foreach($table->getElementsByTagName('tr') as  $key =>$tr){
     $tr->getElementsByTagName('td')->item(0)->nodeValue;
}

但是我想通过getElementById获取表。

but I want to get table by getElementById. Is it possible by using table's id?

推荐答案

实际上你需要先做这个:

Actually you need to do this first:

$doc->validateOnParse = true;

然后

$tableId = 'someId';
$table = $dom->getElementById($tableId);

foreach($table->getElementsByTagName('tr') as  $key =>$tr){
     $tr->getElementsByTagName('td')->item(0)->nodeValue;
}

这篇关于使用getElementById可以实现相同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆