简体   繁体   English

将元素从一个位置移动到另一位置

[英]Moving element from one position to another

In a container I have four rows. 在一个容器中,我有四行。 Each with a different number of columns. 每个都有不同数量的列。 Whenever I click on each row it should move to its next row. 每当我单击每一行时,它都应移至下一行。 However on clicking the final row (any row which comes to the final position) it should move to the first row. 但是,在单击最后一行(到达最终位置的任何行)时,它应移至第一行。

I tried the insertAfter() method in jQuery but I was not able to move the final row. 我在jQuery中尝试了insertAfter()方法,但无法移动最后一行。 Please suggest me best way to move the final row. 请建议我最好的方法来移动最后一行。

I have the current setup in my page: 我的页面中有当前设置:

$(".row").each(function() {
  $(this).insertAfter($(this).next());

You need to check if next() returns an element. 您需要检查next()返回一个元素。 If it doesn't, use the first. 如果不是,请使用第一个。 Something like this: 像这样:

$(".row").each(function() {
  var $row = $(this);
  var $target = $row.next();
  if (!$target.length)
    $target = $row.siblings().first();

  $row.insertAfter($target);
});

It's possible you can simplify the siblings().first() call, but that would depend on how your HTML is structured. 您可以简化siblings().first()调用,但这取决于HTML的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM