为什么物品在排序后失去对齐?
我尝试再次编写css
代码-没有成功。
有帮助吗?
function sort_tags(){
let a = $('#selSort').val();
if(a == 'asc'){
$('.tag').sort(function(a, b) {
if (a.textContent < b.textContent) {return -1;}
else{return 1;}
}).appendTo('#parent');
}
else if(a == 'desc'){
$('.tag').sort(function(a, b) {
if (a.textContent < b.textContent) {return 1;}
else{return -1;}
}).appendTo('#parent');
}
$('.tag').css('display', 'inline-block');
$('#parent').css('text-align-last', 'justify');
}
.parent{
text-align:justify;
text-align-last:justify;
}
.tag{
display:inline-block;
padding:2px 9px;
background:gold;
margin:9px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='parent' id='parent'>
<div class='tag'>aaa</div>
<div class='tag'>bbb</div>
<div class='tag'>ccc</div>
<div class='tag'>ddd</div>
</div>
<select class='selTop' id='selSort' onchange='sort_tags()'>
<option value='asc'>SORT ABC</option>
<option value='desc'>SORT CBA</option>
</select>
答案 0 :(得分:1)
您可以使用flex
.parent{
display:flex;
justify-content: space-between;
}
function sort_tags(){
let a = $('#selSort').val();
if(a == 'asc'){
$('.tag').sort(function(a, b) {
if (a.textContent < b.textContent) {return -1;}
else{return 1;}
}).appendTo('#parent');
}
else if(a == 'desc'){
$('.tag').sort(function(a, b) {
if (a.textContent < b.textContent) {return 1;}
else{return -1;}
}).appendTo('#parent');
}
}
.parent{
display:flex;
justify-content: space-between;
}
.tag{
display:inline-block;
padding:2px 9px;
background:gold;
margin:9px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='parent' id='parent'>
<div class='tag'>aaa</div>
<div class='tag'>bbb</div>
<div class='tag'>ccc</div>
<div class='tag'>ddd</div>
</div>
<select class='selTop' id='selSort' onchange='sort_tags()'>
<option value='asc'>SORT ABC</option>
<option value='desc'>SORT CBA</option>
</select>