【发布时间】:2014-10-19 22:34:44
【问题描述】:
我正在用primefaces 5开发一个项目,一页使用需要jquery的js文件,但显示Uncaught TypeError: undefined is not a function,但是当我将我的jquery源从<h:outputScript library="primefaces" name="jquery/jquery.js" target="head"/>更改为
<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 默认包含 jqueryui 吗?看起来您的第一个版本没有包含 ui 库,这意味着
draggable未定义 -
感谢引导的答案,当用户按下右键时我必须添加一个便笺,解码我上面发布的代码是 js 便笺代码,如果我使用 primefaces jquery,contextMenu 会检测到正确单击但粘滞便笺代码不运行,如果我使用 jquery 链接粘滞便笺代码工作但上下文菜单没有,这是笔记代码工作:pastie.org/9504320 这是 contextMenu 工作:@987654322 @ 在最后一个便签显示 Uncaught TypeError: undefined is not a function .
-
BalusC 我在网上找到了代码,这是真的。 google chrome 调试器在此行中显示错误 $('span.delete').live('click', function()
-
我刚从“live”转到“on”并且可以工作,但现在我必须刷新页面以调用删除方法
-
这是另一个问题,但原版已修复,非常感谢,我会更加注意版本。请创建一个答案以设置为已解决
标签: javascript jquery jsf jsf-2 primefaces