如何通过部分 class 名称获取元素列表?
How to get list of elements by partial class name?
我有一个 HTML 文档,其中有一个 table 多行定义为:
<tr class="row_1"></tr>
<tr class="row_2"></tr>
...
<tr class="row_10"></tr>
总行数未知。
是否可以获取所有以class名称开头的元素(行)row_
?
"Is it possible to get all the elements (rows) that start with the class name row_?
"
当然,这是可能的。在使用 HAP 时,您可以 使用 XPath 或 LINQ 来表达您的查询:
HtmlDocument doc;
....
....
var resultXPath = doc.DocumentNode
.SelectNodes("//tr[starts-with(@class, 'row_')]");
var resultLINQ = doc.DocumentNode
.Descendants("tr")
.Where(o => o.GetAttributeValue("class","").StartsWith("row_"));
我有一个 HTML 文档,其中有一个 table 多行定义为:
<tr class="row_1"></tr>
<tr class="row_2"></tr>
...
<tr class="row_10"></tr>
总行数未知。
是否可以获取所有以class名称开头的元素(行)row_
?
"Is it possible to get all the elements (rows) that start with the class name
row_?
"
当然,这是可能的。在使用 HAP 时,您可以 使用 XPath 或 LINQ 来表达您的查询:
HtmlDocument doc;
....
....
var resultXPath = doc.DocumentNode
.SelectNodes("//tr[starts-with(@class, 'row_')]");
var resultLINQ = doc.DocumentNode
.Descendants("tr")
.Where(o => o.GetAttributeValue("class","").StartsWith("row_"));