相比之前学习的HtmlDocument类和HtmlElement类,mshtml还提供了网页元素更详细的分类,比如
IHTMLScriptElement :脚本元素
IHTMLStyleSheet :样式表
IHTMLFormElement:表单元素
等等
获得脚本信息:
Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click Dim doc As mshtml.IHTMLDocument doc = wbMain.Document.DomDocument Dim scrs As mshtml.IHTMLElementCollection = doc.scripts Dim scr As mshtml.IHTMLScriptElement For i As Integer = 0 To scrs.length - 1 scr = CType(scrs.item(i),mshtml.IHTMLScriptElement) txtInfo.Text &= "htmlFor:" & scr.htmlFor & vbCrLf txtInfo.Text &= "event:" & scr.event & vbCrLf txtInfo.Text &= "src:" & scr.src & vbCrLf txtInfo.Text &= "text:" & scr.text & vbCrLf txtInfo.Text &= "type:" & scr.type & vbCrLf txtInfo.Text &= "====================" & vbCrLf Next End Sub
运行如下:
获得样式表信息:
Private Sub Button4_Click(sender As Object,e As EventArgs) Handles Button4.Click Dim doc As mshtml.IHTMLDocument2 doc = wbMain.Document.DomDocument Dim styles As mshtml.HTMLStyleSheetsCollection = doc.styleSheets Dim style As mshtml.IHTMLStyleSheet For i As Integer = 0 To styles.length - 1 style = CType(styles.item(i),mshtml.IHTMLStyleSheet) txtInfo.Text &= "cssText:" & style.cssText & vbCrLf txtInfo.Text &= "href:" & style.href & vbCrLf txtInfo.Text &= "id:" & style.id & vbCrLf txtInfo.Text &= "title:" & style.title & vbCrLf txtInfo.Text &= "type:" & style.type & vbCrLf txtInfo.Text &= "====================" & vbCrLf Next End Sub
运行如下:
获得表单信息:
Private Sub Button5_Click(sender As Object,e As EventArgs) Handles Button5.Click Dim doc As mshtml.HTMLDocument doc = wbMain.Document.DomDocument doc.sc Dim eles As mshtml.IHTMLElementCollection = doc.forms Dim frm As mshtml.IHTMLFormElement For i As Integer = 0 To eles.length - 1 frm = CType(eles.item(i),mshtml.IHTMLFormElement) txtInfo.Text &= "action:" & frm.action & vbCrLf txtInfo.Text &= "encoding:" & frm.encoding & vbCrLf txtInfo.Text &= "method:" & frm.method & vbCrLf txtInfo.Text &= "name:" & frm.name & vbCrLf txtInfo.Text &= "target:" & frm.target & vbCrLf txtInfo.Text &= "====================" & vbCrLf Next End Sub
运行如下:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录