vb.net 教程 12-4 msHtml 3

相比之前学习的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 教程 目录

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...