加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp-classic – 带参数的vbscript:getref

发布时间:2020-12-16 06:47:27 所属栏目:asp.Net 来源:网络整理
导读:谁有经验将参数传递给使用getref调用的函数? 下面的代码只是一个例子,dons’t不工作,如何将参数传递给mySub子? button id="myBtn"Click me/buttonscript type="text/vbscript" document.getElementById("myBtn").onclick=GetRef("mySub") Sub mySub(parame
谁有经验将参数传递给使用getref调用的函数?
下面的代码只是一个例子,dons’t不工作,如何将参数传递给mySub子?

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

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

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

解决方法

首先看 at this article about event handling(任何人都知道更好的参考?)来获取上下文:

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.

然后试试这个.hta:

<html>
 <!-- !! https://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,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>

查看

>那个/如何指定没有参数的Sub来按字面意思或通过GetRef处理点击(A或B)
>你可以使用一个参数化的Sub来处理许多按钮的点击,因为引擎将文字代码放入一个匿名的Sub(没有参数)(C / D)
>你不能使用GetRef(“SubWithLotsOfParms”)来设置onClick属性 – 它需要没有参数的s Sub
>你可以让一个没有参数的命名Sub(例如Magic)做引擎匿名的工作;这个Sub然后可以与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 handler in html code -->
   <button id="bttG" onClick="mySub">G</button>

测试.hta.

WRT彼得的建议 – 保持简单是值得的:

Option Explicit
Sub WithLotsOfParms(a,b,c,d)
  WScript.Echo Join(Array(a,d))
End Sub
Dim f : Set f = GetRef("WithLotsOfParms")
WithLotsOfParms 1,2,3,4
f               1,4

输出:

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

您使用GetRef()设置的变量名称与您使用文字子/函数名称完全一样,可能已在昨天建立.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读