在 html 中查找 body 标记结尾的最佳方法
我正在编写一个程序来向html文件添加一些代码
我将使用一系列indexof和循环来查找本质上是“”X的内容 (其中 X 是我正在寻找的位置)
我想到可能有一种更雄辩的方式来做到这一点
,有人有任何建议吗?
目前的样子
<body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.display='block';">
完成后应该是什么样子
<body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.display='block';">
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-9xxxxxx-1");
pageTracker._trackPageview();
} catch(err) {}</script>
I'm writing a program to add some code to html files
I was going to use a series of indexof and loops to find what is essentially ""X
(where X is the spot im looking for)
It occurred to me that there might be a more eloquent way of doing this
does anyone have any suggestions.
what it looks like currently
<body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.display='block';">
what it should look like when im done
<body onLoad="JavaScript:top.document.title='Abraham L Barbrow'; if (self == parent) document.getElementById('divFrameset').style.display='block';">
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-9xxxxxx-1");
pageTracker._trackPageview();
} catch(err) {}</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不确定我是否理解你的意思,但你是这个意思吗?
很明显我不懂 C#...您可能想通过正则表达式使“body”标记不区分大小写。
I'm not sure I'm understanding you, but do you mean this?
And it's probably obvious I don't know c#... You'd probably want to make the "body" tag case insensitive via regexps.
我建议使用 HtmlAgilityPack 将 html 解析为 DOM 并使用它。
I would recommend to use HtmlAgilityPack to parse the html into DOM and work with it.
如果 HTML 文件是有效的 XHTML,您始终可以使用 XmlDocument 类来解释它。 然后,您可以轻松查找 body 元素并向其附加一个子元素。 这会将元素放置在结束 标记之前。
If the HTML files are valid XHTML you could always use the XmlDocument class to interpret it. You could then easily look for the body element and append a child element to it. This would place the element right before the closing </body> tag.
您可能想了解如何使用 Html Agility Pack
http://www.codeplex.com/htmlagilitypack
You might want to look at using the Html Agility Pack
http://www.codeplex.com/htmlagilitypack
我不确定您要在标记后添加的示例内容是否正确,但如果是,我会看到两个问题:
希望这对您有所帮助:)
I'm not sure whether the example content you want to add after the tag is the correct one or not, but if it is, I'm seeing two problems:
Hope that's of some help :)
这就是我得到的,
请随意提出建议
This is what i got
feel free to make suggestions