在html页面中搜索特定标签 [英] Search for a particular tag in a html page
本文介绍了在html页面中搜索特定标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在html文件中搜索特定标签?例如,如果我的html页面的中间位置有h1标签,那么如何以编程方式确保我的页面具有该特定标签?
How can I search for a particular tag in a html file? For example if my html page has h1 tag somewhere in the middle, how to ensure programmatically that my page has that particular tag?
推荐答案
一旦加载了文档,只需执行string.IndexOf("<mytag>")</mytag>
.如果该方法返回的值小于0,则说明您所寻找的字符串不存在.它比使用DOM更快.
Once you get your document loaded, just do a string.IndexOf("<mytag>")</mytag>
. If the method returns anything less that 0, the string you were looking for doesn''t exist. It''s faster than using the DOM stuff.
您可以尝试使用此DOM方法..
x.getElementById(id)-获取具有指定ID的元素
x.getElementsByTagName(name)-获取具有指定标签名称的所有元素
x是节点对象(HTML元素)
hi TRY USING this DOM methods..
x.getElementById(id) - get the element with a specified id
x.getElementsByTagName(name) - get all elements with a specified tag name
x is a node object (HTML element)
indexOf应该按照提示工作,仅对于替代,您可以使用string.Contains()方法.
indexOf should work as told, just for an alternative you could use string.Contains() method.
string str = "<h1>some_unnecessary_string</h1>";
包含方法的示例:
Example for contains method:
bool result = str.Contains("<h1>");
或用于完整标签搜索的正则表达式:
or regular expressions for a complete tag search:
bool result = check(str);
bool check(string source)
{
return new Regex("<h1>.*?</h1>").IsMatch(source);
}
这篇关于在html页面中搜索特定标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文