javascript - 如何解决与 primefaces jquery 的冲突

标签 javascript jquery jsf jsf-2 primefaces

我用 primefaces 5 开发一个项目,一页使用了一个需要 jquery 的 js 文件,但显示 Uncaught TypeError: undefined is not a function ,但是当我从 <h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/> 更改我的 jquery 源时到

<h:head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
</h:head>

工作正常,但我失去了许多 primefaces 功能,如 contexMenu

我该如何解决这个冲突?

这是我的 javascript 文件:

(function($, $S) {
//$ jQuery
//$S window.localStorage
//Variables Declaration
var $board = $('#notesForm\\:board'),
//Board where the Posticks are sticked
Postick, //Singleton Object containing the Functions to work with the LocalStorage
len = 0, //Length of Objects in the LocalStorage
currentNotes = '', //Storage the html construction of the posticks
o; //Actual Postick data in the localStorage

//Manage the Posticks in the Local Storage
//Each postick is saved in the localStorage as an Object 
Postick = {
    add : function(obj) {
        obj.id = $S.length;
        $S.setItem(obj.id, JSON.stringify(obj));
    },

    retrive : function(id) {
        return JSON.parse($S.getItem(id));
    },

    remove : function(id) {
        $S.removeItem(id);
    },

    removeAll : function() {
        $S.clear();
    }

};

//If exist any postick, Create it/them
len = $S.length;
if (len) {
    for (var i = 0; i < len; i++) {
        //Create all posticks saved in localStorage
        var key = $S.key(i);
        o = Postick.retrive(key);
        currentNotes += '<div class="postick"';
        currentNotes += ' style="left:' + o.left;
        currentNotes += 'px; top:' + o.top;
        //data-key is the attribute to know what item delete in the localStorage
        currentNotes += 'px"><div class="toolbar"><span class="delete" data-key="'
                + key;
        currentNotes += '">x</span></div><div contenteditable="true" class="editable">';
        currentNotes += o.text;
        currentNotes += '</div>';
    }

    //Append all the posticks to the board
    $board.html(currentNotes);
}

//When the document is ready, make all posticks Draggable
$(document).ready(function() {
    $(".postick").draggable({
        cancel : '.editable',
        "zIndex" : 3000,
        "stack" : '.postick'
    });
});

//Remove Postick
$('span.delete').live('click', function() {
    if (confirm('Are you sure you want to delete this Note?')) {
        var $this = $(this);
        //data-key is the attribute to know what item delete in the localStorage
        Postick.remove($this.attr('data-key'));
        $this.closest('.postick').fadeOut('slow', function() {
            $(this).remove();
        });
    }
});

//Create postick
$('#notesForm\\:btn-addNote')
        .click(
                function() {
                    $board
                            .append('<div class="postick" style="left:20px;top:70px"><div class="toolbar"><span class="delete" title="Close">x</span></div><div contenteditable class="editable"></div></div>');
                    $(".postick").draggable({
                        cancel : '.editable'
                    });
                });

//Save all the posticks when the user leaves the page
window.onbeforeunload = function() {
    //Clean the localStorage
    Postick.removeAll();
    //Then insert each postick into the LocalStorage
    //Saving their position on the page, in order to position them when the page is loaded again
    $('.postick').each(function() {
        var $this = $(this);
        Postick.add({
            top : parseInt($this.position().top),
            left : parseInt($this.position().left),
            text : $this.children('.editable').text()
        });
    });
}
})(jQuery, window.localStorage);

最佳答案

作为基于 jQuery 的 JSF 组件库的 PrimeFaces 已经随 jQuery(和 UI)开箱即用。一旦您在页面上使用 PrimeFaces 组件,PrimeFaces 就会自行加载它们。绝对没有必要手动加载另一个 jQuery 文件。它只会像您所经历的那样在所有颜色中发生冲突。

如果您确实想在不一定包含 PrimeFaces 组件的页面上使用 jQuery/UI,因此 jQuery/UI 不一定会自动包含,那么您需要手动包含 PrimeFaces 捆绑的 jQuery,如下所示:

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />
<h:outputScript library="primefaces" name="jquery/jquery-plugins.js" target="head" />

注意:这不会以重复包含结束。 JSF/PrimeFaces 能够检测到您已经手动声明了它们并且不会自动包含另一个。

或者,如果您确实想升级 PrimeFaces 捆绑的 jQuery/UI 到更新版本,则将这些文件完全放在以下位置和文件名:

WebContent
 |-- resources
 |    `-- primefaces
 |         `-- jquery
 |              |-- jquery.js          <-- newer jQuery version
 |              `-- jquery-plugins.js  <-- newer jQuery UI version
 :

当在 WAR 而不是 JAR 中找到 JSF 资源时,WAR 中的资源将比 JAR 中的资源具有更高的加载优先级。

如果您仍然遇到 Uncaught TypeError 错误,请确保您在 Internet 上找到的 jQuery 片段与 PrimeFaces 捆绑的 jQuery 版本兼容。 PrimeFaces 5.0 附带 jQuery 1.11.0 和 jQuery UI 1.10.3。您的代码片段似乎是针对 jQuery 1.4 的,并且在指向 jQuery 1.11 的路径中很多已更改。例如 $.live() 在 1.7 中弃用并在 1.9 中删除。另见 the documentation .

关于javascript - 如何解决与 primefaces jquery 的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508564/

相关文章:

javascript - javascript静态类继承非静态类的表现

javascript - Twitter Typehead.JS - 使用 AJAX 调用的自定义 div

java - 使用SEAM 2.1.2和JBOSS 4.2.3GA的组合框中出现错误

forms - 如何创建 h :selectOneRadio with options of a java list (JSF)?

jsf/primefaces 在 bean 初始化期间加载指示器

javascript - Unity3D - 将图像从 PC 内存上传到 WebGL 应用程序

javascript - Jade 没有正确渲染流体宽度等距的 div

javascript - 如何让我的 JavaScript 文件在我的组件结束时执行?

javascript - HTML 表获取选中的复选框和文本元素

jquery - 从 Struts 2 文件上传实用程序发布 ajaxForm 在 IE 中不起作用