首次查看 < a href="" "http://msdn.microsoft.com/ en-us/library/ms974564.aspx" rel="nofollow" 有关事件处理 (有人知道更好的参考吗? ) 的文章以获取上下文 :
The code provided in the onclick attribute will be called when the
user clicks on the text enclosed in the span. This mechanism is great
for small snippets of code, but it becomes cumbersome when you have a
lot of script. This event mechanism works with both VBScript 和
JScript.
What happens behind the scenes is that Internet Explorer calls into
the script engine with the script code 和 tells the engine to create
an anonymous function (a function with no name). Those of you who know
VBScript are probably wondering how it does this, since VBScript
doesn t support anonymous functions. VBScript actually creates a
subroutine called "anonymous" containing the script 和 returns a
pointer to the function that is then hooked up to the event.
然后实验这个.hta:
<html>
<!-- !! http://stackoverflow.com/questions/10741292/vbscript-getref-with-parameter
-->
<head>
<title>GetRef HTA</title>
<HTA:APPLICATION
APPLICATIONNAME="GetRef HTA"
>
<SCRIPT Language="VBScript">
Sub SetClickH和lers()
Set bttB.onClick = GetRef("NoParmsBttB")
Set bttE.onClick = GetRef("Magic")
Set bttF.onClick = GetRef("Magic")
End Sub
trivial h和ler, literally set
Sub NoParmsBttA()
Log "NoParmsBttA() called."
End Sub
trivial h和ler, set via GetRef
Sub NoParmsBttB()
Log "NoParmsBttB() called."
End Sub
one h和ler for many buttons, literally set
Sub h和leClickCD(oBtt)
Log "h和leClickCD() called; you clicked " & oBtt.id
End Sub
one h和ler for many buttons, set via Magic() & GetRef
Sub h和leClickEF(oBtt, dtWhen)
Log "h和leClickEF() called; you clicked " & oBtt.id & " at " & CStr(dtWhen)
End Sub
stuffed via GetRef into onClick
Sub Magic()
h和leClickEF Me, Now
End Sub
Sub Log(s)
MsgBox s, 0, Now
End Sub
</SCRIPT>
</head>
<body onLoad="SetClickH和lers">
<!-- literal onClick h和ler in html code -->
<button id="bttA" onClick="NoParmsBttA">A</button>
<!-- no literal onClick h和ler, will be set by SetClickH和lers via GetRef() -->
<button id="bttB">B</button>
<!-- literal onClick h和lers with parameter (Me, i.e. the Button) -->
<button id="bttC" onClick="h和leClickCD Me">C</button>
<button id="bttD" onClick="h和leClickCD Me">D</button>
<!-- Two params h和ler via SetClickH和lers & Magic -->
<button id="bttE">E</button>
<button id="bttF">F</button>
</body>
</html>
查看
- that/how you can specify a Sub with no params to h和le clicks literally or via GetRef (A resp. B)
- that you can use one parameterized Sub to h和le clicks on many buttons, because the engine puts the literal code into an anonymous Sub (with no params) (C/D)
- that you can t use GetRef("SubWithLotsOfParms") to set the onClick attribute - it needs s Sub with no params
- that you can let a named Sub with no params (e.g. Magic) do the work of the engine s anonymous one; this Sub then can be used with GetRef
WRT Salman A回答:
如果您真的需要错误消息, 例如 :
---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?
Line: 54
Error: Wrong number of arguments or invalid property assignment: mySub
---------------------------
Yes No
---------------------------
然后,你只需要加上:
Sub mySub(parameter)
alert(parameter.toString())
End Sub
和
<!-- literal onClick h和ler in html code -->
<button id="bttG" onClick="mySub">G</button>
测试。 。 hta 。
WRT Peter 的提议,
Option Explicit
Sub WithLotsOfParms(a, b, c, d)
WScript.Echo Join(Array(a, b, c, d))
End Sub
Dim f : Set f = GetRef("WithLotsOfParms")
WithLotsOfParms 1, 2, 3, 4
f 1, 2, 3, 4
输出 :
cscript 01.vbs
1 2 3 4
1 2 3 4
您可以使用 GetRef () 的变量集的名称, 正如您使用 literal Sub/Function 名称 一样, 昨天可能已经建立 。