Monitor the events of elements in the web page

zhaozj2021-02-08  239

Readers who are familiar with the script know the events that can add script response elements, such as hyperlinks onclick events, the ONMOUSEMOVE event of the image, and we can also respond to these events. Below is the code for the Click event of the button in the VB response page: First create a new project, add a webbrowser control in Form1, then add the following code in Form1: Option Explicit Public Sub Some_Procedure () msgbox "You clicked the button. "End Sub Private Sub Form_Load () 'empty download page WebBrowser1.Navigate2" about: blank "End Sub Private Sub WebBrowser1_DocumentComplete (ByVal pDisp As Object, URL As Variant)' established incident response class Dim cfForward As clsForward 'is defined in the browser The HTML code is displayed, which contains a button BTNmyButton Dim Shtml as string shtml = "

This is some text. "

SHTML = SHTML & "

And Here is a button. "

SHTML = SHTML & "

"SHTML = SHTML &" Click this Button. "

'Write HTML code to your browser

Webbrowser1.document.body.innerhtml = SHTML

'Connect the event response class to the page button BTNMyButton

Set cfforward = New CLSFORWARD

Cfforward.set_destination me, "Some_Procedure"

WebBrowser1.document.all ("btNmyButton"). OnClick = Cfforward

End Sub

Add a class module to the project, the Class Module's Name property is set to CLSForward, add the following code to CLSForward:

Option expedition

DIM OOBJECT As Object

DIM SMETHOD AS STRING

Dim binstantiated as boolean

Private sub coplass_initialize ()

BinStantiated = false

End Sub

Public SUB Set_Destination (OinObject As Object, SINMETHOD AS STRING)

Set OOBJECT = OinObject

Smethod = SINMETHOD

BINSTANTIATED = TRUE

End Sub

Public Sub my_default_method ()

IF binstantiated then

CallbyName Oubject, Smethod, VBMethod

END IF

End Sub

This is some text. "

SHTML = SHTML & "

And Here is a button. "

SHTML = SHTML & "

"SHTML = SHTML &" Click this Button. "

'Write HTML code to your browser

Webbrowser1.document.body.innerhtml = SHTML

'Connect the event response class to the page button BTNMyButton

Set cfforward = New CLSFORWARD

Cfforward.set_destination me, "Some_Procedure"

WebBrowser1.document.all ("btNmyButton"). OnClick = Cfforward

End Sub

Add a class module to the project, the Class Module's Name property is set to CLSForward, add the following code to CLSForward:

Option expedition

DIM OOBJECT As Object

DIM SMETHOD AS STRING

Dim binstantiated as boolean

Private sub coplass_initialize ()

BinStantiated = false

End Sub

Public SUB Set_Destination (OinObject As Object, SINMETHOD AS STRING)

Set OOBJECT = OinObject

Smethod = SINMETHOD

BINSTANTIATED = TRUE

End Sub

Public Sub my_default_method ()

IF binstantiated then

CallbyName Oubject, Smethod, VBMethod

END IF

End Sub

Before running, you need MY_DEFAULT_METHOD to set to the default process, the method is: position the cursor to my_default_method in the code window of the CLSForward module, then select Menu Tools | Procedure Attributes. Then click the Advanced button in the pop-up window, then select [Default] in the ProCedure ID drop-down box.

Run the program, click the "Click this Button" button in WebBrowser. The program will pop up the message box prompt "You click on the button."

Www.applevb.com

转载请注明原文地址:https://www.9cbs.com/read-1177.html

New Post(0)