簡體   English   中英

如何解決與primefaces jquery的沖突

[英]How to solve a conflict with primefaces jquery

我開發了一個帶有primefaces 5的項目,一個頁面使用需要jquery的js文件,但是顯示Uncaught TypeError: undefined is not a function ,但當我從<h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/>更改我的jquery源代碼時<h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/> to

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

PrimeFaces作為一個基於jQuery的JSF組件庫已經開箱即用了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中已刪除。 另請參閱文檔

我也喜歡你的問題。 確保script聲明如下例所示。 不要用$(....)唱歌。 使用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:outputScript library="primefaces" name="jquery/jquery.js" target="head"/>
    <script type="text/javascript">
        jQuery(document).ready(function(jQuery){

            ..don't use $

        });
    </script>       
</h:head>

多個不同版本的jQuery共存

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM