vbscript:带参数的getref [英] vbscript: getref with parameter

查看:13
本文介绍了vbscript:带参数的getref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人将参数传递给使用 getref 调用的函数?下面的代码只是一个例子,不起作用,如何将参数传递给mySub子?

has anyone experience with passing a parameter to a function called with getref ? Following code is just an example, doens't work, how to pass the parameter to the mySub sub?

<button id="myBtn">Click me</button>

<script type="text/vbscript">
  document.getElementById("myBtn").onclick=GetRef("mySub") 

  Sub mySub(parameter)
   alert(parameter)
  End Sub
</script>

推荐答案

先看 在这篇关于事件处理的文章(有人知道更好的参考资料吗?)获取上下文:

First look at this article about event handling (anybody knows a better reference?) to get the context for:

onclick 属性中提供的代码将在用户单击包含在跨度中的文本.这个机制很棒对于一小段代码,但是当你有一个很多脚本.此事件机制适用于 VBScript 和JScript.

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 and JScript.

幕后发生的事情是 Internet Explorer 调用带有脚本代码的脚本引擎并告诉引擎创建一个匿名函数(一个没有名字的函数).知道的你们VBScript 可能想知道它是如何做到这一点的,因为 VBScript不支持匿名函数.VBScript 实际上创建了一个包含脚本的名为匿名"的子程序并返回一个指向随后连接到事件的函数的指针.

What happens behind the scenes is that Internet Explorer calls into the script engine with the script code and 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 and returns a pointer to the function that is then hooked up to the event.

然后用这个 .hta 做实验:

Then experiment with this .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 SetClickHandlers()
     Set bttB.onClick = GetRef("NoParmsBttB")
     Set bttE.onClick = GetRef("Magic")
     Set bttF.onClick = GetRef("Magic")
   End Sub
   ' trivial handler, literally set
   Sub NoParmsBttA()
     Log "NoParmsBttA() called."
   End Sub
   ' trivial handler, set via GetRef
   Sub NoParmsBttB()
     Log "NoParmsBttB() called."
   End Sub
   ' one handler for many buttons, literally set
   Sub handleClickCD(oBtt)
     Log "handleClickCD() called; you clicked " & oBtt.id
   End Sub
   ' one handler for many buttons, set via Magic() & GetRef
   Sub handleClickEF(oBtt, dtWhen)
     Log "handleClickEF() called; you clicked " & oBtt.id & " at " & CStr(dtWhen)
   End Sub
   ' stuffed via GetRef into onClick
   Sub Magic()
     handleClickEF Me, Now
   End Sub
   Sub Log(s)
     MsgBox s, 0, Now
   End Sub
  </SCRIPT>
 </head>
  <body onLoad="SetClickHandlers">
   <!-- literal onClick handler in html code -->
   <button id="bttA" onClick="NoParmsBttA">A</button>
   <!-- no literal onClick handler, will be set by SetClickHandlers via GetRef() -->
   <button id="bttB">B</button>
   <!-- literal onClick handlers with parameter (Me, i.e. the Button) -->
   <button id="bttC" onClick="handleClickCD Me">C</button>
   <button id="bttD" onClick="handleClickCD Me">D</button>
   <!-- Two params handler via SetClickHandlers & Magic -->
   <button id="bttE">E</button>
   <button id="bttF">F</button>
 </body>
</html>

去看

  1. 那个/如何指定一个没有参数的 Sub 来按字面意思或通过 GetRef(A 和 B)处理点击
  2. 您可以使用一个参数化的 Sub 来处理对许多按钮的点击,因为引擎将文字代码放入匿名 Sub(没有参数)(C/D)中
  3. 您不能使用 GetRef("SubWithLotsOfParms") 来设置 onClick 属性 - 它需要没有参数的 Sub
  4. 你可以让一个没有参数的命名 Sub(例如 Magic)来完成引擎匿名引擎的工作;这个 Sub 然后可以与 GetRef 一起使用

WRT Salman A 的回答:

WRT Salman A's answer:

如果您确实需要这样的错误消息:

If you really need an error message like:

---------------------------
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 handler in html code -->
   <button id="bttG" onClick="mySub">G</button>

测试.hta.

WRT Peter 的提议 - 保持简单是值得的:

WRT Peter's proposal - it pays to keep it simple:

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() 设置的变量名称与您使用文字 Sub/Function 名称完全一样,这可能是昨天建立的.

That you use the name of the variable set with GetRef() exactly as you use the literal Sub/Function name could have been established yesterday.

这篇关于vbscript:带参数的getref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆