php 如何使用 getElementById

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9616361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:16:08  来源:igfitidea点击:

How to use getElementById

phpdom

提问by mustafa

I want to get the values inside tds. I can do it by getElementsByTagName but I could not manage it by using getElementById.

我想获取 tds 中的值。我可以通过 getElementsByTagName 来完成,但我无法通过使用 getElementById 来管理它。

The HTML might look like this:

HTML 可能如下所示:

<table id="myid">
<tr>
<td>value1</td>
<td>value2</td>
</tr>
<tr>
<td>value1</td>
<td>value2</td>
</tr>
</table>

The php used to access the values is:

用于访问值的 php 是:

<?PHP
$dom = new DOMDocument();
$dom->loadHTMLfile('http://remoteDomain/thispage.html');
$table=$dom->getElementById('myid');

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

EDIT

编辑

I got the error: Fatal error: Call to a member function getElementsByTagName() on a non-object in ...

我收到错误: 致命错误:调用成员函数 getElementsByTagName() 在非对象上...

EDIT2

编辑2

Php info:

php信息:

DOM/XML enabled
DOM/XML API Version 20031129
libxml Version 2.7.3

支持 DOM/XML 的
DOM/XML API 版本 20031129
libxml 版本 2.7.3

Operating system: Windows

操作系统:Windows

回答by

The problem is that getElementByIdneeds a DOCTYPE. If you add

问题是getElementById需要一个DOCTYPE。如果添加

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

at the beginning of the file, it should work (you should also add html and body tags).

在文件的开头,它应该可以工作(您还应该添加 html 和 body 标签)。

Edit:Also you need to put $dom->validateOnParse = true;before you load the HTML file.

编辑:您还需要$dom->validateOnParse = true;在加载 HTML 文件之前放置。

This is apparently a "feature" of the DomDocumentclass, see http://php.net/manual/en/domdocument.getelementbyid.php(comments)

这显然是DomDocument该类的“功能” ,请参阅http://php.net/manual/en/domdocument.getelementbyid.php(评论)

回答by reach4thelasers

you need quotes on your id attribute

你需要在你的 id 属性上加引号

<table id="myid">

回答by MrCode

getElementByIdas the name suggests, will only work if the element you're targeting actually has an id.

getElementById顾名思义,只有当您定位的元素实际上具有id.

回答by Rolice

As I understand, you want to use in foreach getElementById, and your code is working.

据我了解,您想在 foreach 中使用 getElementById,并且您的代码正在运行。

As it could be seen there is difference in the name getElement[S]ByTagNameand getElementById, with purpose.

可以看出,名称getElement[S]ByTagName和有区别getElementById,目的不同。

The definition of idis so to say to be unique identifier of element in a page, so only one id(unique) value is assumed to persist on single page and this value cannot be assigned to another element id. If you have more than one element with the same id value, it is wrong (since HTML 4 or XHTML 1.0, I think).

id的定义可以说是页面中元素的唯一标识符,因此假设只有一个id(唯一)值会保留在单个页面上,并且该值不能分配给另一个元素 id。如果您有多个具有相同 id 值的元素,那就错了(我认为是从 HTML 4 或 XHTML 1.0 开始)。

The way you use, looks valid.

您使用的方式看起来有效。

EDIT

编辑

In this case you may have an anwser here: Same problem already solved

在这种情况下,您可能在这里有一个答案:同样的问题已经解决