将VBscript函数作为参数传递给jQuery click()函数

时间:2015-08-06 22:23:19

标签: javascript jquery internet-explorer vbscript

我正在创作一些HTML文件,这些文件将由嵌入在第三方应用程序中的IE控件呈现。

为了提供一些相当复杂的UI元素,用最少量的烦恼,我选择用jQuery在JavaScript中完成我的大部分编码。这通常很有效,但是主机应用程序提供了一个大型的支持函数库,我需要调用它来触发某些操作(例如打印和数据库更新)。这些支持函数都在外部VBScript(.vbs)文件中,我可以将其包含在HTML文件的标题中。

调用VBscript函数并从我的JavaScript函数中访问全局变量直接完成时可以完美地工作,但我无法弄清楚如何将对VBScript中定义的函数的引用传递给jQuery事件处理程序。

例如,以下代码:

 1: <script type="text/vbscript">
 2:   Function handleClick
 3:      MsgBox "Clicked."
 4:   End Function
 5: </script>
 6:
 7: <script type="text/javascript">
 8:   $(function () {
 9:      $("#clickableThing1").click(handleClick);
10:   });
11: </script>

...在第9行产生以下错误:

  

“对象不支持此属性或方法”

然而,使用以下内容替换第9行可以按预期工作:

$("#clickableThing1").click(function() {
   handleClick();
});

我已尝试将handleClick传递给click()同时document.handleClickthis.handleClick无效。

虽然我当然可以继续在匿名JavaScript函数中包装VBScript调用,但这似乎不必要地冗长,因为肯定这些函数必须在DOM 某处定义,对吧?

有谁能告诉我如何直接引用这些VBScript函数?

1 个答案:

答案 0 :(得分:1)

I'm not sure that you can. I think if you try, the VBScript function is just going to get evaluated in place and its return value will be passed into click().

I'll offer another solution, though it may not meet your requirements. You can use the jQuery attr() function to assign an inline handler to the DOM element.

In my experience, when using VBScript functions as event handlers, you need to specify them using parens or include a language="vbscript" attribute on the DOM element.

<script type="text/javascript">
$(function() {

    // This should work (using parens)...
    $("#clickableThing1").attr("onclick", "handleClick()");

    // or, so should this (no parens on function call)...
    $("#clickableThing1").attr("language", "vbscript").attr("onclick", "handleClick");
});
</script>
相关问题