1

我正在尝试在控件中使用 jQuery 插件。控件可以位于的页面通过 UpdatePanel 使用部分回发。我在控件的 PreRender 事件中包含 jQuery 和插件,如下所示:

ScriptManager.RegisterClientScriptInclude(
    this,
    this.GetType(),
    "jquery",
    "/_infrastructure/javascript/jquery.js"));

ScriptManager.RegisterClientScriptInclude(
    this,
    this.GetType(),
    "jquery.customPlugin",
    "/_infrastructure/javascript/jquery.customPlugin.js");

customPlugin jQuery 插件设置了一个名为“executeCustomPlugin”的新函数。稍后在控件的 PreRender 事件中,我在控件上的一个元素上使用插件:

ScriptManager.RegisterStartupScript(
    this,
    this.GetType(),
    "customPlugin init script",
    @"$(document).ready(function() {
        $('#elementId').executeCustomPlugin();
    });",
    true);

但是,当它执行时,我收到 JavaScript 错误:

$('#elementId').executeCustomPlugin is not a function

看起来好像 jQuery 插件根本没有执行,但我在 jQuery.customPlugin.js 文件中设置了 window.alerts,它确实正在执行。

有没有办法解决这个问题?

4

2 回答 2

0

一种可能的解释是,在执行 ScriptManager 代码之前,您的插件在 DOM 中不存在。

在页面呈现到浏览器后查看页面的源代码,并确保在向脚本管理器注册的 javascript之前呈现自定义插件的脚本标记。

于 2010-02-04T20:43:13.043 回答
0

事实证明,我的问题是由两次包含 jQuery 引起的。第一个 jQuery 实例正在应用插件,但第二个 jQuery 实例正在接收调用。

于 2010-02-04T23:32:05.337 回答