【问题标题】:Possible to intercept onchange event?可以拦截 onchange 事件吗?
【发布时间】:2012-12-19 08:50:23
【问题描述】:

在 VBA 中是否可以拦截 javascript onchange 事件并在 webbrowser 控件中获取原始 ID#?

<select name="status0" id="status0" onchange="javascriptonchangeeventhere">

上面的代码是一个组合框演示,如果我从组合框中选择一个项目,它会在javascript中处理onchange。它所做的是提取一个包含当前日期和参考号的表单,而不是我想提取我自己的用户表单,然后将该数据吐到相关的(notes0)字段中。当它发生时我将如何拦截它,以便我可以将它分配给正确的字段。

【问题讨论】:

  • 没有实用的方法吗?或者甚至确定发生 onchange 事件的位置?

标签: vba


【解决方案1】:

这是一个简短的例子。为“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 sub 然后更改第二个选择的值或单击 IE 页面上的“Javascript”链接,您应该会在 VB 编辑器的立即窗口中看到输出。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2020-01-23
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多