如何从 Internet Explorer DOM 调用 javascript

发布于 2024-12-01 14:54:26 字数 761 浏览 1 评论 0原文

我正在使用 VBA (Excel) 来操作 Internet Explorer 中加载的网页中的下拉框。下拉框附加了一些 JavaScript,以便当下拉选择更改时页面的 UI 控件也会更改。

我使用下面的函数(自动)从下拉列表中选择一个选项。它会更改下拉框的值,但不会触发 UI 更改(与我手动更改下拉框时相比)。

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub

那么我怎样才能触发附加到这个下拉框的javascript呢?或者一般来说,如何通过 MS Internet Explorer 提供的 API 以编程方式触发事件?谢谢。

I'm using VBA (Excel) to manipulate a dropdown box in web page loaded in Internet Explorer. The dropdown box has some javascript attached so that the page's UI controls will change when the dropdown selection is changed.

I use the below function to (automatically) select an option from the dropdown. It changes the value of the dropbox but it does not trigger the UI changes (as compared to when I manually change the dropdown box).

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub

So how can I trigger the javascript that is attached to this dropdown box? Or in general, how can I fire an event programmatically via the API provided by MS Internet Explorer? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-12-08 14:54:26

您应该显示足够的 HTML,以便我们可以看到附加的 javascript 和事件类型。

假设这是一个“onclick”,

你可以有

opt.Selected = True
opt.FireEvent "onclick"

You should show enough HTML that we can see the javascript attached and the type of event.

Lets say it is an "onclick"

You could have

opt.Selected = True
opt.FireEvent "onclick"
还在原地等你 2024-12-08 14:54:26

当您设置组合框值时显式调用您的 javascript 函数。

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            'Call hear javascript function.
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub

call your javascript function explicitly when you set combobox value.

Sub selectFromDropdown(ByRef dropdown As HTMLSelectElement, ByVal optionName As String)
    Dim opts
    Dim opt As HTMLOptionElement

    dropdown.Focus
    dropdown.setCapture
    Set opts = dropdown.children
    For i = 1 To opts.Length
        Set opt = opts.Item(i)
        If opt.Text = optionName Then
            opt.Selected = True
            dropdown.selectedIndex = opt.Index
            'Call hear javascript function.
            Exit For
        End If
    Next
    dropdown.releaseCapture
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文