JQuery 在页面加载时重新定位 div 元素

发布于 2024-08-01 22:11:53 字数 276 浏览 3 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

深居我梦 2024-08-08 22:11:53

您可以通过使用单个 appendToprependTo 调用(哪个调用没有区别)来完成此操作 - 只要您可以首先构建一个特殊排序的 jQuery 对象。

使用与 Marve 的答案相同的标记:

$(function() {
  $('#widgetC').
    add('#widgetE').
    add('#widgetA').
    add('#widgetD').
    add('#widgetB').
    appendTo('#widgets');
});

您可能需要从一个空的 jQuery 对象开始(使用 $([]))并迭代 cookie 中的值而不是像我上面所做的那样能够链接事情。

其实仔细想想...

$(function() {
  var order = 'CEADB'; // from cookie
  $(
    $.map(order.split(''), function(letter) {
      return $('#widget' + letter);
    })
  ).appendTo('#widgets');
});

You can do this with using a single appendTo or prependTo 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:

$(function() {
  $('#widgetC').
    add('#widgetE').
    add('#widgetA').
    add('#widgetD').
    add('#widgetB').
    appendTo('#widgets');
});

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...

$(function() {
  var order = 'CEADB'; // from cookie
  $(
    $.map(order.split(''), function(letter) {
      return $('#widget' + letter);
    })
  ).appendTo('#widgets');
});
御守 2024-08-08 22:11:53

@RwL 的答案中显示的技术将会起作用,但您不必显式地从 DOM 中删除该元素。

使用 .append() 会将元素移动到新位置。

此 HTML:

<div id="widgets">
    <div id="widgetA">A</div>
    <div id="widgetB">B</div>
    <div id="widgetC">C</div>
    <div id="widgetD">D</div>
    <div id="widgetE">E</div>
</div>

应用此脚本:

$(function() {
    var widgetOrder = ['#widgetC','#widgetE','#widgetA','#widgetD','#widgetB'];
    for (var widgetIndex = 0; widgetOrder.length; widgetIndex++) {
        var singleWidget = widgetOrder[widgetIndex];
        $('#widgets').append($(singleWidget));
    }
});

将生成此 HTML:

<div id="widgets">
    <div id="widgetC">C</div>
    <div id="widgetE">E</div>
    <div id="widgetA">A</div>
    <div id="widgetD">D</div>
    <div id="widgetB">B</div>
</div>

您应该能够使用 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:

<div id="widgets">
    <div id="widgetA">A</div>
    <div id="widgetB">B</div>
    <div id="widgetC">C</div>
    <div id="widgetD">D</div>
    <div id="widgetE">E</div>
</div>

With this script applied:

$(function() {
    var widgetOrder = ['#widgetC','#widgetE','#widgetA','#widgetD','#widgetB'];
    for (var widgetIndex = 0; widgetOrder.length; widgetIndex++) {
        var singleWidget = widgetOrder[widgetIndex];
        $('#widgets').append($(singleWidget));
    }
});

Will result in this HTML:

<div id="widgets">
    <div id="widgetC">C</div>
    <div id="widgetE">E</div>
    <div id="widgetA">A</div>
    <div id="widgetD">D</div>
    <div id="widgetB">B</div>
</div>

You should be able to adapt this code using a jQuery cookie plugin like this or this to meet your requirements.

书信已泛黄 2024-08-08 22:11:53

我认为没有一个 jQuery 方法可以用来执行此操作,但似乎您可以通过将每个 DIV 克隆到变量、从 dom 中删除原始版本并附加克隆版本来轻松完成此操作按所需顺序返回正文(使用三个标准 jQuery 方法):

var thisOne = $('div.thisOne').clone();
var thatOne = $('div.thatOne').clone();
$('div.thisOne, div.thatOne').remove();
$('body').append(thatOne + thisOne);

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):

var thisOne = $('div.thisOne').clone();
var thatOne = $('div.thatOne').clone();
$('div.thisOne, div.thatOne').remove();
$('body').append(thatOne + thisOne);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文