jquery 保存元素位置(序列化?)

发布于 2024-07-22 02:24:11 字数 732 浏览 1 评论 0原文

如何保存 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 技术交流群。

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

发布评论

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

评论(2

我们的影子 2024-07-29 02:24:11

我在我的一个项目中使用了类似的函数,并且我已经准备好一段代码来恢复排序。 该函数有 2 个参数:

  1. 列表容器
  2. 保存逗号分隔 ID 的 cookie。

我用它来重新排序基于 ul 的列表,但在你的情况下也应该可以正常工作...

// Function that restores the list order from a cookie
function restoreOrder( _list, _cookie ) {

    var list = $( '#' + _list );
    if( list == null ) return;

    // fetch the cookie value (saved order)
    var cookie = $.cookie( _cookie );

    if( !cookie ) return;

    // make array from saved order
    var IDs = cookie.split( "," );

    // fetch current order
    var items = list.sortable( "toArray" );

    // make array from current order
    var rebuild = new Array();
    for( var v = 0, len = items.length; v < len; v++ )
        rebuild[items[v]] = items[v];

    for( var i = 0, n = IDs.length; i < n; i++ ) {

        // item id from saved order
        var itemID = IDs[i];

        if( itemID in rebuild ) {

            // select item id from current order
            var item = rebuild[itemID];

            // select the item according to current order
            var child = $( '#' + _list ).children( '#' + item );

            // select the item according to the saved order
            var savedOrd = $( '#' + _list ).children( '#' + itemID );

            // remove all the items
            child.remove();

            // add the items in turn according to saved order
            // we need to filter here since the "ui-sortable"
            // class is applied to all ul elements and we
            // only want the very first!  You can modify this
            // to support multiple lists - not tested!
            $( '#' + _list ).filter( ':first' ).append( savedOrd );

        } // if

    } // for

} // restoreOrder

我忘记了我从哪里得到它的(谷歌搜索时出现的一些论坛或博客)...但应归功于原始内容作者。 我对原始例程进行了一些修改,通过接受这些参数使其更加可重用。

干杯,
微观^地球

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:

  1. The list container
  2. The cookie that holds the comma separated IDs.

I use it to reorder ul based lists, but should work fine in your case too...

// Function that restores the list order from a cookie
function restoreOrder( _list, _cookie ) {

    var list = $( '#' + _list );
    if( list == null ) return;

    // fetch the cookie value (saved order)
    var cookie = $.cookie( _cookie );

    if( !cookie ) return;

    // make array from saved order
    var IDs = cookie.split( "," );

    // fetch current order
    var items = list.sortable( "toArray" );

    // make array from current order
    var rebuild = new Array();
    for( var v = 0, len = items.length; v < len; v++ )
        rebuild[items[v]] = items[v];

    for( var i = 0, n = IDs.length; i < n; i++ ) {

        // item id from saved order
        var itemID = IDs[i];

        if( itemID in rebuild ) {

            // select item id from current order
            var item = rebuild[itemID];

            // select the item according to current order
            var child = $( '#' + _list ).children( '#' + item );

            // select the item according to the saved order
            var savedOrd = $( '#' + _list ).children( '#' + itemID );

            // remove all the items
            child.remove();

            // add the items in turn according to saved order
            // we need to filter here since the "ui-sortable"
            // class is applied to all ul elements and we
            // only want the very first!  You can modify this
            // to support multiple lists - not tested!
            $( '#' + _list ).filter( ':first' ).append( savedOrd );

        } // if

    } // for

} // restoreOrder

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

囚你心 2024-07-29 02:24:11

好吧,如果您只需要记住顺序,您可以将逗号分隔的 id 字符串插入 cookie 中吗? 并在页面加载时将它们拉回。

据我所知,序列化仅适用于表单数据,不适用于 DOM 元素。

为了解决您的评论,您可以使用简单的 jquery 从页面构建一个 CSV:

var list = '';
$('#productlist .sortableproduct').each(function() {
    list += this.attr('id') + ',';
});

然后将列表保存到 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:

var list = '';
$('#productlist .sortableproduct').each(function() {
    list += this.attr('id') + ',';
});

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.

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