HTML Agility Pack - 如何在 Head 元素的顶部附加元素?
我正在尝试使用 HTML Agility Pack 将脚本元素附加到 html 的 HEAD 部分的顶部。到目前为止我看到的示例只是使用 AppendChild(element) 方法来完成此操作。我需要将附加到头部的脚本放在其他一些脚本之前。我该如何指定这一点?
这就是我正在尝试的:
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.Load(filePath);
HtmlNode head = htmlDocument.DocumentNode.SelectSingleNode("/html/head");
HtmlNode stateScript = htmlDocument.CreateElement("script");
head.AppendChild(stateScript);
stateScript.SetAttributeValue("id", "applicationState");
stateScript.InnerHtml = "'{\"uid\":\"testUser\"}'";
我希望将脚本标签添加到 HEAD 的顶部而不是附加在末尾。
I'm trying to use HTML Agility Pack to append a script element into the top of the HEAD section of my html. The examples I have seen so far just use the AppendChild(element)
method to accomplish this. I need the script that I am appending to the head section to come before some other scripts. How can I specify this?
Here's what I'm trying:
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.Load(filePath);
HtmlNode head = htmlDocument.DocumentNode.SelectSingleNode("/html/head");
HtmlNode stateScript = htmlDocument.CreateElement("script");
head.AppendChild(stateScript);
stateScript.SetAttributeValue("id", "applicationState");
stateScript.InnerHtml = "'{\"uid\":\"testUser\"}'";
I would like a script tag to be added toward the top of HEAD rather than appended at the end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
意识到这是一个老问题,也有可能预先考虑当时可能不存在的子元素。
Realizing that this is an old question, there is also the possibility of prepending child elements that might not have existed then.
明白了..
HtmlNode 有以下方法:
Got it..
HtmlNode has the following methods: