的VBScript:getref与参数 [英] vbscript: getref with parameter

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

问题描述

与传递参数给调用函数getref人的经验?
以下code是只是一个例子,好好尝试工作,如何将参数传递到mySub子

 <按钮ID =myBtn>点击ME< /按钮><脚本类型=文/ VBScript中>
  的document.getElementById(myBtn)。的onclick = GetRef(mySub)  子mySub(参数)
   警报(参数)
  结束小组
< / SCRIPT>


解决方案

首先看在这篇文章中关于事件处理(任何人知道一个更好的参考?)来获取上下文:


  

在onclick属性提供的code将被调用时,
  用户点击封闭在跨度的文本。这种机制是伟大的
  为code小片段,但是当你有一个就变得很麻烦
  很多剧本。此事件机制适用于VBScript和
  JScript的。


  
  

会发生什么幕后的是Internet Explorer调用到
  与脚本脚本引擎code和告诉引擎打造
  一个匿名函数(没有名字的函数)。你们谁知道
  VBScript中可能想知道它是如何做到这一点,因为VBScript中
  不支持匿名函数。实际上VBScript的创建
  子程序称为匿名包含脚本,并返回一个
  指针随后被挂​​接到该事件的函数。


然后用这个实验.HTA:

 < HTML和GT;
 <! - !! http://stackoverflow.com/questions/10741292/vbscript-getref-with-parameter
  - >
 < HEAD>
  <标题> GetRef HTA< /标题>
  < HTA:APPLICATION
    APPLICATIONNAME =GetRef HTA
  >
  < SCRIPT LANGUAGE =VBScript中>
   子SetClickHandlers()
     设置bttB.onClick = GetRef(NoParmsBttB)
     设置bttE.onClick = GetRef(魔术师)
     设置bttF.onClick = GetRef(魔术师)
   结束小组
   琐碎的处理程序,从字面上设置
   子NoParmsBttA()
     日志NoParmsBttA()调用。
   结束小组
   琐碎的处理程序,通过GetRef设置
   子NoParmsBttB()
     日志NoParmsBttB()调用。
   结束小组
   对于许多按钮一个处理程序,从字面上设置
   子handleClickCD(oBtt)
     LOGhandleClickCD()调用;您点击与& oBtt.id
   结束小组
   一处理程序的按钮,通过魔术()及设置; GetRef
   子handleClickEF(oBtt,dtWhen)
     LOGhandleClickEF()调用;您点击与& oBtt.id&安培; 在与& CStr的(dtWhen)
   结束小组
   通过GetRef装进的onClick
   子魔()
     handleClickEF我,现在
   结束小组
   子登录(多个)
     MSGBOX S,0,现在
   结束小组
  < / SCRIPT>
 < /头>
  <身体的onLoad =SetClickHandlers>
   <! - 字面的onClick处理程序的HTML code - >
   <按钮ID =bttA的onClick =NoParmsBttA>将< /按钮>
   <! - 没有文字的onClick处理程序,将被SetClickHandlers通过GetRef()设置 - >
   <按钮ID =BTTB> B< /按钮>
   <! - 字面的onClick处理程序与参数(箱,即按钮) - GT;
   <按钮ID =bttC的onClick =handleClickCD我> C< /按钮>
   <按钮ID =bttD的onClick =handleClickCD我> D< /按钮>
   <! - 通过SetClickHandlers和放两个参数的处理程序;魔术 - >
   <按钮ID =bttE> E< /按钮>
   <按钮ID =BTTF> F< /按钮>
 < /身体GT;
< / HTML>


  1. 的/你怎么可以没有PARAMS字面上或通过GetRef(A RESP。B)处理点击指定子

  2. 您可以使用一个参数化的子来处理许多按钮点击,因为发动机使字面code到一个匿名子(无PARAMS)(C / D)

  3. ,你不能用GetRef(SubWith​​LotsOfParms)来设置onclick属性 - 它需要亚健康没有PARAMS

  4. ,你可以让没有PARAMS(例如魔术)命名的子做的工作,发动机的匿名之一;本款则可以GetRef使用

WRT萨​​尔曼A的回答是:

如果你真的需要这样的错误消息:

  ---------------------------
错误
---------------------------
已发生运行时错误。
您希望调试?行:54
错误:参数或无效的属性赋值错误号码:mySub
---------------------------
是否
---------------------------

,那么你只需要添加:

 子mySub(参数)
     警报(parameter.toString())
   结束小组

 <! - 字面的onClick处理程序的HTML code  - >
   <按钮ID =BTTG的onClick =mySub> G< /按钮>

要测试.HTA。

WRT彼得的建议 - 它支付保持简单:

 显式的选项
子WithLotsOfParms(A,B,C,D)
  WScript.Echo加入(阵列(A,B,C,D))
结束小组
昏暗F:将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(设置变量的名称)完全一样,你可以使用已经建立的字面子/函数名昨天。

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:

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.

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.

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>

to see

  1. that/how you can specify a Sub with no params to handle clicks literally or via GetRef (A resp. B)
  2. that you can use one parameterized Sub to handle clicks on many buttons, because the engine puts the literal code into an anonymous Sub (with no params) (C/D)
  3. that you can't use GetRef("SubWithLotsOfParms") to set the onClick attribute - it needs s Sub with no params
  4. 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'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   
---------------------------

then you just have to add:

   Sub mySub(parameter)
     alert(parameter.toString())
   End Sub

and

   <!-- literal onClick handler in html code -->
   <button id="bttG" onClick="mySub">G</button>

to the test .hta.

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

output:

cscript 01.vbs
1 2 3 4
1 2 3 4

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天全站免登陆