XMLWriter 无法写入元素
我发现不可能将任何内容(文本或其他元素)写入head元素,例如
$XML = new XMLWriter();
$XML->openMemory();
$XML->startElement("head");
$XML->writeAttribute("id","head");
$XML->text("lable");
$XML->endElement();
$XML->startElement("div");
$XML->text("div");
$XML->endElement();
echo $XML->outputMemory();
将在body中输出元素,而不是在head中,但它会产生正确的属性:
<html>
<head id="head">
</head>
<body>
lable
<div>
div
</div>
</body>
</html>
为什么我不能将任何内容写入head?
Iv discovered that it is impossible to write any content (text or another elements) into head element, for example
$XML = new XMLWriter();
$XML->openMemory();
$XML->startElement("head");
$XML->writeAttribute("id","head");
$XML->text("lable");
$XML->endElement();
$XML->startElement("div");
$XML->text("div");
$XML->endElement();
echo $XML->outputMemory();
will output elements in body, not in head, but it makes correct attributes:
<html>
<head id="head">
</head>
<body>
lable
<div>
div
</div>
</body>
</html>
Why i cannot write any content into head?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTML 不支持在其
中出现文本或内容相关元素。来自W3C 建议:
通常,您想要写入 HTML 头部的主要元素是</code> 元素和任何 <code><meta></code> 元素。我认为以下方法会起作用:
HTML does not support the presence of text or content-related elements in its
<head>
. From the W3C recommendation:Typically, the main elements you would want to write to the HTML head are the
<title>
element and any<meta>
elements. I presume that the following would work:运行与上面提供的完全相同的脚本,我得到以下结果:
由于您的代码甚至没有提到
body
或html
元素,也许有一些后处理PHP 代码生成的输出?Running the exact same script you provided above, I get the following result:
Since your code doesn't even mention a
body
or anhtml
element, maybe there's something post-processing the output generated by your PHP code?