【发布时间】:2011-09-08 15:44:10
【问题描述】:
我将这个插件用于飞出菜单:http://www.filamentgroup.com/lab/jquery_ipod_style_and_flyout_menus/
按钮在这样的 div 内:
<div class="stuff">
some stuff
<a class="quickfire">menu</a>
</div>
我将它应用到这样的一些链接:
jQuery('.quickfire').menu({
content: jQuery('#search-engines').html(), // grab content from this page
showSpeed: 400
});
其中 .quickfire 是链接的类名。到目前为止一切顺利,工作正常。
但是,用户也可以触发 AJAX 调用,该调用将从服务器获取一堆 HTML 并将 div“东西”替换为新内容(其本身将包含一个快速链接)。
jQuery.ajax({
url: 'ajax_file.php',
data: {
action: 'create_option_new_version',
id: jQuery('#qid').val(),
div: jQuery("#addMoreOptions").parent().parent().attr('id'),
cleanOutput: true
},
success: function(data, textStatus, jqXHR){
jQuery(".stuff").html(data);
}
});
正如预期的那样,quickfire 链接不再附加到 jQuery 菜单。所以,我每次都重新链接它:
jQuery.ajax({
url: 'ajax_file.php',
data: {
action: 'create_option_new_version',
id: jQuery('#qid').val(),
div: jQuery("#addMoreOptions").parent().parent().attr('id'),
cleanOutput: true
},
success: function(data, textStatus, jqXHR){
jQuery(".stuff").html(data);
var position = jQuery('.quickfire').position();
console.log("left: " + position.left + " top: " + position.top);
jQuery('.quickfire').menu({
content: jQuery('#search-engines').html(), // grab content from this page
showSpeed: 400
});
}
});
快到了!
问题是,当我点击新创建的快速启动按钮时,它可以工作,但菜单出现在屏幕的左上角,而不是按钮旁边!
我试图打印出快速启动按钮的“位置”。对于初始加载,它说 361 x 527。对于后续加载,它们都说 0 x 320
这是真正的代码:
jQuery("#addMoreOptions").live('click',function(){
jQuery(".lastPollOptionInput").removeClass("lastPollOptionInput");
jQuery.ajax({
url: 'ajax_file.php',
data: {
action: 'create_option_new_version',
id: jQuery('#qid').val(),
div: jQuery("#addMoreOptions").parent().parent().attr('id'),
cleanOutput: true
},
success: function(data, textStatus, jqXHR){
jQuery("#addMoreOptions").parent().parent().html(data);
jQuery('.quickfire').fgmenu({
content: jQuery('#search-engines').html(), // grab content from this page
showSpeed: 400
});
}
});
});
【问题讨论】:
-
我尝试在替换内容之前“分离”快速启动按钮,然后重新插入。现在它没有把菜单放在左上角,而是把菜单放在我调用 ajax 之前的位置(我想这是有道理的)。位置不完全正确,但至少在地理位置上更接近
-
你能链接到你看到这个问题的页面吗?对 jsfiddle (jsfiddle.net/6VrgD/3) 的初步测试表明您的代码中的其他地方可能有问题...
-
不幸的是,它存在于我的计算机上,客户不希望我分享太多内容。但是我添加了一些代码。
-
我设法通过在 quickfire 上调用“remove”,然后手动重新插入它来编写 jquery 代码。
-
您能否使用
.delegate()或.live()来避免重新绑定新创建的.quickfire?
标签: jquery jquery-ui events dom