jquery 保存元素位置(序列化?)
如何保存 div 中的元素位置?
我是否使用序列化,如果是,我该怎么做?
我有这个div:
<div id="produtlist">
<img id="productid_10" src="images/pic10.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_11" src="images/image1.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_12" src="images/image2.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_13" src="images/pic1.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_14" src="images/pic2.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_15" src="images/pic3.jpg" class="sortableproduct" alt="" title="" />
</div>
How do I save elements positions in a div?
Do i use serialize and if yes how do I do it?
I have this div:
<div id="produtlist">
<img id="productid_10" src="images/pic10.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_11" src="images/image1.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_12" src="images/image2.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_13" src="images/pic1.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_14" src="images/pic2.jpg" class="sortableproduct" alt="" title="" />
<img id="productid_15" src="images/pic3.jpg" class="sortableproduct" alt="" title="" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我的一个项目中使用了类似的函数,并且我已经准备好一段代码来恢复排序。 该函数有 2 个参数:
我用它来重新排序基于 ul 的列表,但在你的情况下也应该可以正常工作...
我忘记了我从哪里得到它的(谷歌搜索时出现的一些论坛或博客)...但应归功于原始内容作者。 我对原始例程进行了一些修改,通过接受这些参数使其更加可重用。
干杯,
微观^地球
I use a similar function in one of my projects and I've got a piece of code ready to restore the ordering. The function takes 2 parameters:
I use it to reorder ul based lists, but should work fine in your case too...
I've forgotten where I had got it from (some forum or blog that came up while Googling)... but due credits to the original author. I have modified the original routine a bit to make it more reusable by accepting those parameters.
Cheers,
miCRoSCoPiC^eaRthLinG
好吧,如果您只需要记住顺序,您可以将逗号分隔的 id 字符串插入 cookie 中吗? 并在页面加载时将它们拉回。
据我所知,序列化仅适用于表单数据,不适用于 DOM 元素。
为了解决您的评论,您可以使用简单的 jquery 从页面构建一个 CSV:
然后将列表保存到 cookie,或将其发送回服务器。
将事情恢复到原来的顺序有点困难。 如果将字符串发送回服务器,那么以正确的顺序输出内容会简单得多...但是您可以使用 jquery 中的 DOM 工具来移动 DOM 对象。 无论哪种方式都很简单。
Well if you just need to remember the order, you could just bung a comma-delimited string of the id's into a cookie? And pull them back out on page load.
Serialize only works on form data, as far as I know, and not on DOM elements.
To address your comment, you'd build a CSV from the page using simple jquery:
Then you save list to a cookie, or send it back to the server.
Putting things back in their order is a little tougher. If you send the string back to the server, it would be far simpler to just output things in the right order... But you can use the DOM tools in jquery to move DOM objects around. Simple enough either way.