gpt4 book ai didi

javascript - CSS Grid - 向上移动行的最佳方式?

转载 作者:行者123 更新时间:2023-11-28 14:36:04 24 4
gpt4 key购买 nike

由于 CSS 网格没有行包装器的概念,我该如何实现可以将一行向上移动一行的功能?

通常我会获取一行父级并使用 JS(appendChild 等)改变 dom。但不确定如何在 CSS 网格中执行此操作。至少不是以干净的方式。

我想我可以捕获 6 个 span 并通过使用 JS 将它们全部向上移动来改变 dom,但是用 6 个 span 这样做似乎不太干净。

.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
border-top: 1px solid black;
border-right: 1px solid black;
}

.grid > span {
padding: 8px 4px;
border-left: 1px solid black;
border-bottom: 1px solid black;
}
  <body>
<div class="grid">
<span><strong>Id</strong></span>
<span><strong>Full Name</strong></span>
<span><strong>Country</strong></span>
<span><strong>Created at</strong></span>
<span><strong>Email</strong></span>
<span><strong></strong></span>
<span>0</span>
<span>Aaron Kris</span>
<span>Philippines</span>
<span>1991-05-23T14:19:51</span>
<span>Ophelia_Mitchell@karley.name</span>
<span><button>move row up</button></span>

<span>1</span>
<span>Simeon McLaughlin</span>
<span>Singapore</span>
<span>2012-03-07T00:08:36</span>
<span>Simon@salvatore.biz</span>
<span><button>move row up</button></span>




<span>2</span>
<span>Kelsie Shanahan</span>
<span>Brazil</span>
<span>1985-03-10T20:13:04</span>
<span>Karianne@salvatore.biz</span>
<span><button>move row up</button></span>
</div>
</body>

最佳答案

我试图得到你的结果

$(document).ready(function(e) {
$('button').on('click', function(){
var allspn=$('.grid>span').length;
var selfpos=$(this).parent('span').index();
var span6=$('.grid>span').eq(selfpos);
var span5=$('.grid>span').eq(selfpos-1);
var span4=$('.grid>span').eq(selfpos-2);
var span3=$('.grid>span').eq(selfpos-3);
var span2=$('.grid>span').eq(selfpos-4);
var span1=$('.grid>span').eq(selfpos-5);

if($(this).parent('span').index()>11){
$(span6).insertBefore($('.grid>span').eq(selfpos-11));
$(span5).insertBefore($('.grid>span').eq(selfpos-11));
$(span4).insertBefore($('.grid>span').eq(selfpos-11));
$(span3).insertBefore($('.grid>span').eq(selfpos-11));
$(span2).insertBefore($('.grid>span').eq(selfpos-11));
$(span1).insertBefore($('.grid>span').eq(selfpos-11));
}
})
});
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
border-top: 1px solid black;
border-right: 1px solid black;
}

.grid > span {
padding: 8px 4px;
border-left: 1px solid black;
border-bottom: 1px solid black;
}
</style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
<div class="grid">
<span><strong>Id</strong></span>
<span><strong>Full Name</strong></span>
<span><strong>Country</strong></span>
<span><strong>Created at</strong></span>
<span><strong>Email</strong></span>
<span><strong></strong></span>
<span>0</span>
<span>Aaron Kris</span>
<span>Philippines</span>
<span>1991-05-23T14:19:51</span>
<span>Ophelia_Mitchell@karley.name</span>
<span><button>move row up</button></span>

<span>1</span>
<span>Simeon McLaughlin</span>
<span>Singapore</span>
<span>2012-03-07T00:08:36</span>
<span>Simon@salvatore.biz</span>
<span><button>move row up</button></span>




<span>2</span>
<span>Kelsie Shanahan</span>
<span>Brazil</span>
<span>1985-03-10T20:13:04</span>
<span>Karianne@salvatore.biz</span>
<span><button>move row up</button></span>
</div>
</div>

关于javascript - CSS Grid - 向上移动行的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53492468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com