如何使用 jQuery 创建一个绝对位于另一个元素旁边的 div?
我的页面上有一个搜索框,它连接到一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的搜索框位于名为“搜索”的 div 中。
然后你可以使用 jQuery after 函数来附加你的html:
如果你想创建'div'不是返回的 HTML 的一部分,插入它非常容易:
Say your search box is in a div called 'search'.
Then you can use the jQuery after function to append your html:
If the 'div' you want to create isn't part of the returned HTML, it's pretty easy to insert it:
像这样..(返回html后)
Like this..(after returned html)
我最终这样做了:
I ended up doing this: