为什么 getElementById 在这种情况下不起作用?
我有一段 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( 之前添加了
并在没有 XInclude 的情况下进行了测试,但不起作用。$Document->validateOnParse = TRUE;
$main_html[ 'main' ] );
最后我找到了解决方案,坏行替换为:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题并得到了与上面提到的相同的答案。由于我不知道如何定义 DTD,所以我得到了以下解决方案:
更改 HTML 代码:
进入
如果您仍然需要 id 属性来显示(出于 Javascript 目的),您可以使用以下代码:
我遇到了另一个问题此解决方案因为 xml:id 属性不会通过验证器。
我对此问题的解决方案如下: 保持 HTML 代码不变:
现在像这样更改您的 PHP 代码:
这个解决方案非常适合我。我希望你也觉得它有用:)
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:
into
If you still need the id attribute to apear (for Javascript purposes) you can use the following code:
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:
Now change your PHP code like this:
This solution works perfect for me. I hope you find it useful as well :)
您必须传递定义
id
属性的 DTD。否则,您无法使用getElementById
。有关详细信息,请参阅:
You have to pass a DTD which defines the
id
attribute. Otherwise, you cannot usegetElementById
.For more information, see: