我需要在文档上使用live函数。
我尝试了这段代码:(这不能正常工作。)
感谢您的建议。
$(document).live('ready', function() {
$(".icons").contextMenu(
{
menu: 'menuIcons'
},
function(action, el, pos)
{
contextMenuWork(action, el, pos);
});
function contextMenuWork(action, el, pos) {
switch (action) {
case "open":
{
alert("open");
break;
}
}
}
});
答案 0 :(得分:1)
$(document).live()
没有意义,因为只有一个document
,如果不重新加载页面就永远无法重新创建。
您想致电:
$(document).ready(function() {...
如果已加载document
DOM对象,jQuery将立即调用您的函数。
答案 1 :(得分:1)
试试这个:
function contextMenuWork(action, el, pos) {
switch (action) {
case "open": {
alert("open");
break;
}
}
}
$(".icons:not(.live)").live('click',function(e){
if (e.which === 2) {
e.preventDefault();
$(this).addClass('live').contextMenu({
menu: 'menuIcons'
},
function(action, el, pos) {
contextMenuWork(action, el, pos);
}).trigger({type:'mousedown',button:2}).trigger({type:'mouseup'});
}
});
当右键单击元素时,它使用后期绑定绑定到事件;然后重新触发右键单击事件。
答案 2 :(得分:1)
在您的AJAX代码中,当您知道哪些数据可以在将数据附加到DOM时绑定该数据上的插件
(function() {
var contextMenuWork = function(action, el, pos) {
switch (action) {
case "open":
{
alert("open");
break;
}
}
};
$.ajax({
url: myUrl,
success: function( data ) {
$("body").replaceWith(data); // Example!
$(".icons", data).contextMenu({
menu: 'menuIcons'
}, function(action, el, pos) {
contextMenuWork(action, el, pos);
});
}
});
})();
答案 3 :(得分:0)