下面是一個簡短的例子。項目引用的 「Microsoft HTML對象庫」 和 「Microsoft Internet控制」
類模塊中設置 「clsEvents」:
Option Explicit
Public WithEvents slct As MSHTML.HTMLSelectElement
Public WithEvents href As MSHTML.HTMLAnchorElement
'Note that having defined "href" as "WithEvents", if you choose "href"
' from the left-hand dropdown at the top of the class code module
' then the right-hand dropdown will populate with events you can select
' from to create an event-handling procedure in the class
Private Function href_onclick() As Boolean
Debug.Print "link clicked"
href_onclick = False 'cancels the navigation
End Function
Private Sub slct_onchange()
Debug.Print "select onchange - value is " & slct.Value
End Sub
Private Function slct_onclick() As Boolean
Debug.Print "select onclick - value is " & slct.Value
End Function
在常規模塊:
Option Explicit
Dim evt As clsEvents
Sub Setup()
Dim IE As New InternetExplorer
Dim el As Object, el2 As Object
Set evt = New clsEvents
IE.Visible = True
IE.navigate "http://www.csee.wvu.edu/~riggs/html/select_example.html"
Do While IE.Busy
Loop
Set el = IE.document.getElementsByTagName("select")(1)
Set el2 = IE.document.getElementsByTagName("a")(1)
If Not el Is Nothing Then
Debug.Print "setting event capture: currentvalue=" & el.Value
Set evt.slct = el
End If
If Not el2 Is Nothing Then
Debug.Print "setting event capture on link:" & el2.innerText
Set evt.href = el2
End If
End Sub
如果運行Setup
子然後更改第二個選擇的值或單擊IE中的頁面上的「Javascript」鏈接,您應該在VB編輯器的立即窗口中看到輸出。
希望能幫助你開始。
有沒有實際的方法來做到這一點?甚至確定一個onchange事件發生在哪裏? –