【问题标题】:Unable to call a plugin function outside document.ready event无法在 document.ready 事件之外调用插件函数
【发布时间】:2015-02-09 13:40:06
【问题描述】:

我正在使用 jquery e JSF 来构建我的应用程序的页面。在每个 ajax 请求之后,我需要绑定一些函数,比如掩码、表单消息等。 我的问题是在$(function() 之外,我无法访问插件:

(function($) {
    // call setMask function on the document.ready event
    $(function() {
      // calling a function, the plugin is undefined.
        initMsgForm();
     // calling here, it works.
        if (typeof $.fn.areYouSure !== 'undefined') {
            $('#myForm').areYouSure({
                'message' : 'You have unsaved data'
            });
        }
    });
})(jQuery);

function initMsgForm() {
if (typeof $.fn.areYouSure !== 'undefined') {
    $('#myForm').areYouSure({
        'message' : 'Você tem informações não salvas'
    });
}

}

为什么我无法访问我的函数中的插件?

编辑: 这两个函数都在同一个文件“main.js”中 我包含 js 文件的 xhtml:

<h:head>
<f:facet name="first">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title></title>
</f:facet>

<f:facet name="middle">
    <h:outputStylesheet name="bootstrap/css/bootstrap.css" />

    <h:outputStylesheet name="css/dashboard.css" />
    <h:outputStylesheet name="css/main.css" />

    <script
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" />
    <script
        src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js" />
    <script
        src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.6.0/ui-bootstrap-tpls.min.js" />
    <h:outputScript name="js/jquery.are-you-sure.js" />
    <h:outputScript name="bootstrap/js/bootstrap.js" />
    <h:outputScript name="js/meiomask.min.js" />
    <h:outputScript name="js/main.js" />
</f:facet>

<f:facet name="last">
    <script
        src="https://cdnjs.cloudflare.com/ajax/libs/jquery.meiomask/1.1.14/meiomask.min.js" />
    <h:outputStylesheet name="css/font-awesome.css" />
</f:facet>

【问题讨论】:

  • 如果您此时没有看到插件,则说明您没有正确加载它们。不过,这个问题实在是太零碎了,无法得到有用的回答。
  • 另请注意:您可以使用 jQuery(function($){YOUR CODE USING A LOCAL $ HERE}); 快​​捷语法将 IIFE 与 DOM-ready 结合起来。两者合二为一。
  • 听起来插件在不应该加载的情况下加载或正在使用 DOM-ready 回调。您需要显示其余代码。
  • @TrueBlueAussie 查看我的编辑。

标签: javascript jquery jsf


【解决方案1】:

我发现了问题。我试图加载 jQuery。默认情况下,primefaces 已经加载了 jQuery,所以我包含了两次。我删除了 jQuery 声明,现在它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2018-08-13
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多