繁体   English   中英

从服务器端调用立即调用函数内部的函数

[英]calling a function inside Immediately-Invoked Function from server side - jquery

我用以下内容包围了我的脚本

(function ($)
{  
    $(document).ready(function () {....})
    function HandleOpJqUIClientSide(){....}
    .............

})(jQuery);

以及将以下内容嵌入到ASP页面中

<script src="~/Scripts/StatsScript.js" type="text/javascript">jQuery.noConflict();</script>

我的脚本中有一个函数,该函数在服务器端被调用,如下所示

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "HandleOpJqUIClientSide()", true);

在没有冲突的情况下处理我的脚本之前,它工作正常(由于一些与我的代码冲突的外部插件,我需要它)

现在扔了

未定义的异常

我该如何再次从服务器端调用函数

你必须像这样

jQuery.noConflict();
(function ($)
{  
    $(document).ready(function () {....})
    function HandleOpJqUIClientSide(){....}
    .............

})(jQuery);

从脚本标签中删除noConflict

更新

实际上, HandleOpJqUIClientSide在该范围内是私有的,不能从外部直接访问,我们需要为此设置一个公共访问器。

jQuery.noConflict();
var noConflict = (function ($){  
    $(document).ready(function () {....})
    return {
         HandleOpJqUIClientSide : function (){....}
    }
    .............

})(jQuery);

在服务器端

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "noConflict.HandleOpJqUIClientSide()", true);

您只需要这样做:

<script src = "other_lib.js"> </script>
<script src="jquery.js"></script > 

<script type="text/javascript"> 
$.noConflict();
jQuery(document).ready(function ($) {
    function HandleOpJqUIClientSide() {
        //your $ in the code here will represent JQuery 
    }
});
// Code that uses other library's $ can follow here.
</script>

准备好外部文件:

var $1=JQuery();
function HandleOpJqUIClientSide() {
    //your $1 in the code here will represent JQuery 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM