JQuery 在页面加载时重新定位 div 元素
我正在使用 Interface 和 JQuery 实现 portlet/widget JQuery 接口。 用户拖放小部件,屏幕就可以处理它。 我可以使用序列化来显示屏幕上 div 元素的顺序。 我可以将其保存在 cookie 或 DB 中 - 但这并不重要。
我的问题是如何加载页面并重新定位 div 以匹配 cookie/DB 中的顺序? 一个基于 cookie 的例子会很好,但这并不重要。
是否有某种我可以调用的 JQuery 方法能够按照我想要的顺序重新定位 div?
I am implementing a portlet/widget JQuery interface using Interface and JQuery. The user drags and drops widgets adn the screen can handle it.
I am able to use serialize which displays the order of the div elements on the screen.
I can save this in a cookie or DB - that doesnt matter yet.
My question is how is it possible to load a page repositioning the divs to match the order in the cookie/DB? A cookie based example would be nice, but it doesn't really matter.
Is there a certain JQuery method I can call that is able to reposition the divs in the order I want them to be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过使用单个
appendTo
或prependTo
调用(哪个调用没有区别)来完成此操作 - 只要您可以首先构建一个特殊排序的 jQuery 对象。使用与 Marve 的答案相同的标记:
您可能需要从一个空的
jQuery
对象开始(使用$([])
)并迭代 cookie 中的值而不是像我上面所做的那样能够链接事情。其实仔细想想...
You can do this with using a single
appendTo
orprependTo
call (makes no difference which) - as long as you can build up a specially ordered jQuery object first.Using the same markup as Marve's answer:
You'd probably need to start with an empty
jQuery
object (use$([])
) and iterate over the values from your cookie rather than being able to chain things like I have done above though.Actually, thinking about it...
@RwL 的答案中显示的技术将会起作用,但您不必显式地从 DOM 中删除该元素。
使用
.append()
会将元素移动到新位置。此 HTML:
应用此脚本:
将生成此 HTML:
您应该能够使用 jQuery cookie 插件(例如 这个或这个来满足您的要求。
The technique shown in @RwL's answer will work, but you don't have to explicitly remove the element from the DOM.
Using
.append()
will move the element to its new location.This HTML:
With this script applied:
Will result in this HTML:
You should be able to adapt this code using a jQuery cookie plugin like this or this to meet your requirements.
我认为没有一个 jQuery 方法可以用来执行此操作,但似乎您可以通过将每个 DIV 克隆到变量、从 dom 中删除原始版本并附加克隆版本来轻松完成此操作按所需顺序返回正文(使用三个标准 jQuery 方法):
I don't think there's a single jQuery method that you could use to do this, but it seems like you could get this done pretty easily by cloning each of your DIVs to variables, removing the originals from the dom, and appending the cloned versions back to the body in the desired order (using three standard jQuery methods):