Javascript 重新排列Html元素

Javascript 重新排列Html元素,javascript,jquery,html,Javascript,Jquery,Html,我有四个div元素 <div id="one" class="present" style="display: none;">1</div> <div id="two" class="present" style="display: none;">2</div> <div id="three" class="present" style="display: none;">3</div> <

我有四个div元素

    <div id="one" class="present" style="display: none;">1</div>
    <div id="two" class="present" style="display: none;">2</div>
    <div id="three" class="present" style="display: none;">3</div>
    <div id="four" class="present" style="display: none;">4</div>
<>但是这似乎把最后div的HTML放在空白空间,这不是我想要的。我要他们往上推


ps:这不仅仅是一个可能被删除的值。中间的任何值都可以被删除。即使多个值也可能被删除,这意味着,我只能在最后一个div中有值,理想情况下,我希望将其移动到第一个div,这非常确定会起到作用:

$(".present").each(function(){
  if ($(this).is(":empty")){
    $(this).append($(this).parent());
  }
});
append函数删除元素并将其附加到目标。通过将它附加到它已经在其中的容器中,它应该从dom中删除,并在末尾重新插入,这应该允许较低的对象自然向上移动

如果要设置效果的动画,可以克隆对象而不是附加对象,然后将其第一个实例作为目标,将“高度”值设置为0,然后将其删除


因此,为了在容器上维护正确的ID,您可以用下一个对象的标记替换每个对象的标记,但是其中涉及到相当多的内容篡改。我建议这样做(您需要稍微更改您的id命名方案,以使设置更容易一些。不确定这是否适合您的情况。)

所以你的身份证应该是a1,a2,a3等等。。。这个字母是因为你不能用一个数字来开始一个id或者一个类,但是如果需要的话可以用更具描述性的东西来代替

根据您使用这些ID的方式,您可以完全去掉它们,并在使用这些ID的任何函数中直接检查对象的index()

所以,如果你在做这样的事情:

$("#one").stuff();
。。您可以改为使用:

$(".present:eq(0)").stuff();

当然,如果您在css中引用特定的ID,那么这可能不会很好地工作。

非常肯定这会起到作用:

$(".present").each(function(){
  if ($(this).is(":empty")){
    $(this).append($(this).parent());
  }
});
append函数删除元素并将其附加到目标。通过将它附加到它已经在其中的容器中,它应该从dom中删除,并在末尾重新插入,这应该允许较低的对象自然向上移动

如果要设置效果的动画,可以克隆对象而不是附加对象,然后将其第一个实例作为目标,将“高度”值设置为0,然后将其删除


因此,为了在容器上维护正确的ID,您可以用下一个对象的标记替换每个对象的标记,但是其中涉及到相当多的内容篡改。我建议这样做(您需要稍微更改您的id命名方案,以使设置更容易一些。不确定这是否适合您的情况。)

所以你的身份证应该是a1,a2,a3等等。。。这个字母是因为你不能用一个数字来开始一个id或者一个类,但是如果需要的话可以用更具描述性的东西来代替

根据您使用这些ID的方式,您可以完全去掉它们,并在使用这些ID的任何函数中直接检查对象的index()

所以,如果你在做这样的事情:

$("#one").stuff();
。。您可以改为使用:

$(".present:eq(0)").stuff();

当然,如果您在css中引用特定ID,那么这可能不太管用。

您不能简单地执行以下操作:

$(".present:empty").each(function(index,item) {
    $(this).remove();
    $("#parent").append(this);
});
把它们拉出来,再把它们放回列表的末尾


这里有一个例子来说明

你就不能这么做吗:

$(".present:empty").each(function(index,item) {
    $(this).remove();
    $("#parent").append(this);
});
把它们拉出来,再把它们放回列表的末尾

这里有一个例子来说明

简单的解决方案。。。(或可供选择)

功能检查(id){
var ele=document.getElementById(id),
父节点=ele.parentNode,
len=parent.children.length,
child=null;
对于(变量i=0;i
简单解决方案。。。(或可供选择)

功能检查(id){
var ele=document.getElementById(id),
父节点=ele.parentNode,
len=parent.children.length,
child=null;
对于(变量i=0;i
我在上一段代码中发现了一些问题

从您最初的问题中,我假设您不希望重新排列div元素,而只希望重新排列每个div的html内容

假设你有:

<div id="one" class="present">1</div>
<div id="two" class="present"></div>
<div id="three" class="present">3</div>
<div id="four" class="present"></div>
<div id="five" class="present">5</div>
<div id="six" class="present"></div>
<div id="seven" class="present"></div>
<div id="eight" class="present">8</div>
<div id="nine" class="present"></div>
<div id="ten" class="present"></div>
生成的HTML为:

<div id="one" class="present">1</div>
<div id="two" class="present">3</div>
<div id="three" class="present">5</div>
<div id="four" class="present">8</div>
<div id="five" class="present"></div>
<div id="six" class="present"></div>
<div id="seven" class="present"></div>
<div id="eight" class="present"></div>
<div id="nine" class="present"></div>
<div id="ten" class="present"></div>
1
3.
5.
8.

-使用下一个非空值



正如您在生成的HTML中看到的,div元素没有被重新排序,只是HTML内容被上移,填充了空格,从而将空格保留在底部。它可以处理中间和结尾缺少的多个空值。

我在上一段代码中发现了一些问题

从您最初的问题中,我假设您不希望重新排列div元素,而只希望重新排列每个div的html内容

假设你有:

<div id="one" class="present">1</div>
<div id="two" class="present"></div>
<div id="three" class="present">3</div>
<div id="four" class="present"></div>
<div id="five" class="present">5</div>
<div id="six" class="present"></div>
<div id="seven" class="present"></div>
<div id="eight" class="present">8</div>
<div id="nine" class="present"></div>
<div id="ten" class="present"></div>
生成的HTML为:

<div id="one" class="present">1</div>
<div id="two" class="present">3</div>
<div id="three" class="present">5</div>
<div id="four" class="present">8</div>
<div id="five" class="present"></div>
<div id="six" class="present"></div>
<div id="seven" class="present"></div>
<div id="eight" class="present"></div>
<div id="nine" class="present"></div>
<div id="ten" class="present"></div>
1
3.
5.
8.

-使用下一个非空值



正如您在生成的HTML中看到的,div元素没有被重新排序,只是HTML内容被上移,填充了空格,从而将空格保留在底部。它可以处理中间和结尾缺少的多个空值。

只需在最后一个非空值之后插入所有空值即可

$('.present:empty').insertAfter('.present:not(:empty):last');

编辑:

我没有注意到你只是在移动文本,你可以这样做

var withText = $('.present:not(:empty)').clone();// get all with text
$('.present').text(''); // clear all the text
// loop through and add the number text
$.each(withText, function(i,v){
    $('.present').eq(i).text($(v).text()); 
});

只需在最后一个非空值之后插入所有空值即可

$('.present:empty').insertAfter('.present:not(:empty):last');

编辑:

我没有注意到你只是在移动文本,你可以这样做

var withText = $('.present:not(:empty)').clone();// get all with text
$('.present').text(''); // clear all the text
// loop through and add the number text
$.each(withText, function(i,v){
    $('.present').eq(i).text($(v).text()); 
});

发布了我尝试过并挂起的内容。发布了我尝试过并挂起的内容。是否要将当前html()值设置为下一个()值?一