为什么 getElementById 在这种情况下不起作用?

发布于 2024-12-04 00:52:15 字数 1816 浏览 1 评论 0原文

我有一段 PHP 代码。

PHP:

$Implementation = new DOMImplementation();
$Document = $Implementation->createDocument( NULL, NULL, $Implementation->createDocumentType( 'html' ) );
$Document->encoding = 'utf-8';
$Document->loadXML( $main_html[ 'main' ] ); // load main.html
$Document->xinclude();
$Fragment = $Document->createDocumentFragment();
$Fragment->appendXML( $main_html[ 'page' ] ); // load page.html
$Document->getElementById( 'content' )->appendChild( $Fragment );
...

除了最后一行之外,一切正常,出现错误:

PHP Fatal error:  Call to a member function appendChild() on a non-object

似乎 getElementById() 方法不适用于 $Document

查看 HTML。

HTML (main.html):

...
    </head>
    <body xmlns:xi="http://www.w3.org/2001/XInclude">
        <xi:include href="wrapper.html" />
    </body>
</html>

HTML (wrapper.html):

<section
    id="wrapper"
    xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="header.html" />
    <xi:include href="nav.html" />
    <xi:include href="content.html" />
    <xi:include href="aside.html" />
    <xi:include href="footer.html" />
</section>

HTML (content.html):

<section id="content" />

我在 $Document->loadXML( 之前添加了 $Document->validateOnParse = TRUE; $main_html[ 'main' ] ); 并在没有 XInclude 的情况下进行了测试,但不起作用。

最后我找到了解决方案,坏行替换为:

$Document->getElementsByTagName( 'section' )->item( 1 )->appendChild( $Fragment );

getElementsByTagName() 方法适用于 $Document 但不满足我。我错过了什么吗?

I have piece of PHP code.

PHP:

$Implementation = new DOMImplementation();
$Document = $Implementation->createDocument( NULL, NULL, $Implementation->createDocumentType( 'html' ) );
$Document->encoding = 'utf-8';
$Document->loadXML( $main_html[ 'main' ] ); // load main.html
$Document->xinclude();
$Fragment = $Document->createDocumentFragment();
$Fragment->appendXML( $main_html[ 'page' ] ); // load page.html
$Document->getElementById( 'content' )->appendChild( $Fragment );
...

Everything works well except last line, appears error:

PHP Fatal error:  Call to a member function appendChild() on a non-object

It seems getElementById() method doesn't work for $Document.

Look at the HTML.

HTML (main.html):

...
    </head>
    <body xmlns:xi="http://www.w3.org/2001/XInclude">
        <xi:include href="wrapper.html" />
    </body>
</html>

HTML (wrapper.html):

<section
    id="wrapper"
    xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="header.html" />
    <xi:include href="nav.html" />
    <xi:include href="content.html" />
    <xi:include href="aside.html" />
    <xi:include href="footer.html" />
</section>

HTML (content.html):

<section id="content" />

I added $Document->validateOnParse = TRUE; before $Document->loadXML( $main_html[ 'main' ] ); and tested without XInclude, but doesn`t work.

Finally I found the solution, bad line replace with:

$Document->getElementsByTagName( 'section' )->item( 1 )->appendChild( $Fragment );

getElementsByTagName() method works for $Document but doesn't satisfy me. Did I missed something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-11 00:52:15

我遇到了同样的问题并得到了与上面提到的相同的答案。由于我不知道如何定义 DTD,所以我得到了以下解决方案:

更改 HTML 代码:

<section id="content" />

进入

<section xml:id="content" />

如果您仍然需要 id 属性来显示(出于 Javascript 目的),您可以使用以下代码:

<section id="content" xml:id="content" />

我遇到了另一个问题此解决方案因为 xml:id 属性不会通过验证器。
我对此问题的解决方案如下: 保持 HTML 代码不变:

<section id="content" />

现在像这样更改您的 PHP 代码:

/* ... Add xml:id tags */

$tags = $Document -> getElementsByTagName('*');
foreach ($tags as $tag)
  $tag->setAttribute('xml:id',$tag->getAttribute('id'));

/* ... Here comes your PHP code */

$Document->getElementById( 'content' )->appendChild( $Fragment );

/* ... Remove xml:id tags */

foreach ($tags as $tag)
  $tag->removeAttribute('xml:id');

这个解决方案非常适合我。我希望你也觉得它有用:)

I had the same problem and got the same answer mentioned above. Since I don't know how to define a DTD I got the following solution:

Change the HTML code:

<section id="content" />

into

<section xml:id="content" />

If you still need the id attribute to apear (for Javascript purposes) you can use the following code:

<section id="content" xml:id="content" />

I ran into another problem with this solution since xml:id attribute won't pass validator.
My solution for this problem was as follows: Leave the HTML code as is:

<section id="content" />

Now change your PHP code like this:

/* ... Add xml:id tags */

$tags = $Document -> getElementsByTagName('*');
foreach ($tags as $tag)
  $tag->setAttribute('xml:id',$tag->getAttribute('id'));

/* ... Here comes your PHP code */

$Document->getElementById( 'content' )->appendChild( $Fragment );

/* ... Remove xml:id tags */

foreach ($tags as $tag)
  $tag->removeAttribute('xml:id');

This solution works perfect for me. I hope you find it useful as well :)

源来凯始玺欢你 2024-12-11 00:52:15

您必须传递定义 id 属性的 DTD。否则,您无法使用getElementById

有关详细信息,请参阅:

You have to pass a DTD which defines the id attribute. Otherwise, you cannot use getElementById.

For more information, see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文