html() 调用后元素的 jQuery 位置

发布于 2024-12-03 17:56:36 字数 2977 浏览 0 评论 0原文

我正在使用此插件作为弹出菜单: 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
                });


            }
        });


    });

I am using this plugin for fly-out menus: http://www.filamentgroup.com/lab/jquery_ipod_style_and_flyout_menus/

The button is inside a div like that:

<div class="stuff">
some stuff
<a class="quickfire">menu</a>
</div>

I am applying it to some link like so:

jQuery('.quickfire').menu({ 
        content: jQuery('#search-engines').html(), // grab content from this page
        showSpeed: 400 
    });

Where .quickfire is the class name of the link. So far so good, works.

However the user can also trigger an AJAX call, which will fetch a bunch of HTML from the server and replace the div "stuff" with new content (which itself will contain a quickfire link).

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


            }
        });

As expected, the quickfire link is no longer attached to the jQuery Menu. So, i'm linking it again every time:

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


            }
        });

Almost there!

The issue is that, when I click on the newly created quickfire button, it works, but the menu appears at the top left corner of my screen, instead of next to the button!

I tried to print out the "position" of the quickfire button. For the initial load one, it said 361 x 527. For the subsequent ones, they all say 0 x 320

Here is the real code:

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


            }
        });


    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国产ˉ祖宗 2024-12-10 17:56:36

我会走简单的路。只需在 AJAX 调用之前获取元素的位置:

var x = $('#old-div').offset().left;
var y = $('#old-div').offset().top;

在 AJAX 调用之后,将位置应用于新元素:

$('#new-div').css({ "left" : x, "top" : y });

另外,我不明白菜单是否为position:absolute,或者其他什么。也检查一下。希望我有用,干杯,祝你好运!

I would go the easy way. Just get the position of the element before the AJAX call:

var x = $('#old-div').offset().left;
var y = $('#old-div').offset().top;

After the AJAX call apply the position to the new element:

$('#new-div').css({ "left" : x, "top" : y });

Also, I didn't understand if the menu is position: absolute, or whatever. Check for that as well. Hope I was useful, cheers and good luck!

樱娆 2024-12-10 17:56:36

您是否使用的是 fgmenu() 而不仅仅是 menu()? (更新:您说您自己将该函数从 menu 重命名为 fgmenu,因此它可能在内部调用 $.fn.menu ——换句话说,冲突的 jQuery UI 函数。 ..)

检查控制台是否有错误,以防万一其中有任何明显的错误。

我的下一个猜测是 jQuery("#addMoreOptions").parent().parent() 可能不是您期望的元素,实际上可能是 < html> 元素或其他一些类似的意外节点。

另一件事是,因为您调用 $(something).parent().html("blah") 意味着您正在替换 $(something)节点,该节点在后续调用中可能不存在。

如果有更好的方法来定位并保留对预期节点的引用,我建议您这样做。

Could it be that you're using fgmenu() instead of just menu()? (Update: You said you renamed the function from menu to fgmenu yourself, so it's possible that it's calling $.fn.menu -- in other words, the conflicting jQuery UI function -- internally in places...)

Check the console for errors, just in case there's anything obvious in there.

My next guess would be that jQuery("#addMoreOptions").parent().parent() might not be the element you expect it to be, and could in fact be the <html> element or some other similarly unexpected node.

The other thing is that because you're calling $(something).parent().html("blah") means that you're replacing the $(something) node, which may not exist on subsequent calls.

If there's a better way to locate and keep a reference to the intended node, I would suggest doing that instead.

心凉 2024-12-10 17:56:36

如果元素在重新插入/重新附加后显示在错误的位置,那么您需要检查该对象的 css。 Firebug 是一个用于此目的的简单工具,在处理 CSS 问题时为我节省了无数时间。添加内容时,有两件事发生了变化:

1)新内容的父容器发生了变化,因此菜单元素位置的上下文也发生了变化。
2)菜单的css发生了变化,也许从Absolute变为Relative或Inherit?

通常,在这种情况下,最简单的答案就是正确的,但 Firebug 将是找到它的方法。您可以选择任何元素来查看附加的 css...只需找出更改即可。

http://getfirebug.com/layout

If the element is being shown in the wrong spot after being reinserted / reattached, then you need to check the css of the object. Firebug is an easy tool to use for this purpose, and has saved me countless hours when working with CSS issues. When the content is being readded, either two things have changed:

1) the parent container for the new content has changed, and so the context in how the position of the menu element has changed.
2) the css of the menu has changed, perhaps from Absolute to Relative or Inherit?

Usually, the simplest answer is the correct on in this case, but Firebug will be the way to find it. You can select any element with it to see the css attached to it... just need to find out the change.

http://getfirebug.com/layout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文