如何使用 jQuery 创建一个绝对位于另一个元素旁边的 div?

发布于 2024-09-26 08:24:01 字数 399 浏览 1 评论 0原文

我的页面上有一个搜索框,它连接到一个 AJAX 脚本,该脚本通过 GET 根据搜索框的内容返回 HTML。

为了在搜索框正下方显示结果,我需要在相对于该框的位置创建一个 div(该框的位置根据屏幕分辨率而变化)。我如何使用 jQuery 来实现这一目标?

或者,可以使用 jQuery UI 自动完成来代替吗?如果可以,怎么做?我确实对它很满意,但根本无法让它显示 HTML。

编辑: 这是一个 jsfiddle 链接: http://jsfiddle.net/UxPb8/

你认为它有效是可以原谅的,但一旦有更多页面上搜索框左侧的内容,定位被抛弃,因此出现这个问题。

I have a search box on a page that's hooked up to an AJAX script that returns HTML based on the contents of the search box, via GET.

In order to display the results directly below the search box, I need to create a div in a position relative to that box (the box's position is variable depending on screen resolutions). How can I use jQuery to achieve this?

Alternatively, can jQuery UI Autocomplete be used instead and if so, how? I did have a bash with it but had trouble getting it to display the HTML at all.

Edit:
Here is a jsfiddle link:
http://jsfiddle.net/UxPb8/

You can be forgiven for thinking it works but as soon as there's more stuff on the page to the left of the search box, the positioning is thrown off, hence this question.

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

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

发布评论

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

评论(3

听风吹 2024-10-03 08:24:01

假设您的搜索框位于名为“搜索”的 div 中。

然后你可以使用 jQuery after 函数来附加你的html:

$('#search').after(html);

如果你想创建'div'不是返回的 HTML 的一部分,插入它非常容易:

$('#search').after("<div>" + html + "</div>");

Say your search box is in a div called 'search'.

Then you can use the jQuery after function to append your html:

$('#search').after(html);

If the 'div' you want to create isn't part of the returned HTML, it's pretty easy to insert it:

$('#search').after("<div>" + html + "</div>");
终止放荡 2024-10-03 08:24:01

像这样..(返回html后)

$('.classNameORID').append("<div style='position:relative;'>"+ data +"</div>");

Like this..(after returned html)

$('.classNameORID').append("<div style='position:relative;'>"+ data +"</div>");
八巷 2024-10-03 08:24:01

我最终这样做了:

//calculate the position
var parentDiv = $(this).parents(selector);
var top = parentDiv.position().top + parentDiv.height();
var left = $("#wrapper").width()-427; //427 is a fixed offset - it's still in the right place for any screen res

//run the search
$.ajax({
    url: 'ajax/find.php',
    data: { search: $(this).val() },
    success: function(data) {
        $("#globalFindResults")
            .css({
                'position': 'absolute',
                'top': top,
                'left': left
                })
            .html(data)
            .slideDown('fast');
    }
});

I ended up doing this:

//calculate the position
var parentDiv = $(this).parents(selector);
var top = parentDiv.position().top + parentDiv.height();
var left = $("#wrapper").width()-427; //427 is a fixed offset - it's still in the right place for any screen res

//run the search
$.ajax({
    url: 'ajax/find.php',
    data: { search: $(this).val() },
    success: function(data) {
        $("#globalFindResults")
            .css({
                'position': 'absolute',
                'top': top,
                'left': left
                })
            .html(data)
            .slideDown('fast');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文