【问题标题】:calling a function inside Immediately-Invoked Function from server side - jquery从服务器端调用立即调用函数中的函数 - jquery
【发布时间】:2014-05-22 06:33:56
【问题描述】:

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

(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);

在处理我的脚本之前它工作正常(因为我需要它,因为一些外部插件与我的代码冲突)

现在扔了

未定义的异常

如何再次操作从服务器端调用的函数

【问题讨论】:

  • 添加;在 HandleOpJqUIClientSide();
  • 感谢您的回复,但我有同样的例外

标签: javascript jquery jquery-ui server-side scriptmanager


【解决方案1】:

你必须这样

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);

【讨论】:

  • thx 但我有同样的异常 '0x800a1391 - JavaScript 运行时错误:'HandleOpJqUIClientSide' 未定义'
  • 似乎该功能在该范围内变得不公开(function ($){}(jQuery)
  • @user690069 哎呀,我想到了,但忘了提。查看我更新的代码。
【解决方案2】:

你只需要这样做:

<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 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多