列表的倒序

时间:2019-08-18 06:55:14

标签: javascript css

import SwiftUI

struct ContentView: View {

    @State var shouldShowAlert: Bool = false

    // this never gets called
    func onAlertDismissed() {
        print("you will not see this in the console")
    }

    // this doesn't seem to work
    var dismissButton: some View {

        Button(action: {
            self.onAlertDismissed()
        }) {
            // Bilbo Baggins does not appear -- "OK" still shows
            Text("BILBO BAGGINS")
        }
    }

    var body: some View {

        VStack {
            Spacer()

            Button(action: {
                self.shouldShowAlert = true
            }) {
                Text("show the alert!")
            }
            Spacer()
        }.alert(isPresented: $shouldShowAlert, content: {

            // what to add here?
            Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."))

            // what I have tried and doesn't work:
            /*
             Alert(title: Text("Alert:"), message: Text("press OK to execute onAlertDismissed()..."), dismissButton: self.dismissButton as? Alert.Button)
             */


        })

    }
}
from itertools import combinations

str = "new    search words"

splited = str.split(" ")

output = sum([list(map(list, combinations(splited, i))) for i in range(len(splited) + 1)], [])
print(output)

我想反转此数字列表以成为

div > div {
  text-align: center;
  color: white;
  background: black;
  border: 1px solid white;
}

div.row {
  display: flex;
  flex-direction: row-reverse;
}

我尝试使用

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="row">
  <div class="col-4">1</div>
  <div class="col-4">2</div>
  <div class="col-4">3</div>
  <div class="col-4">4</div>
  <div class="col-4">5</div>
  <div class="col-4">6</div>
  <div class="col-4">7</div>
</div>

但没有按我希望的那样工作

是否可以仅使用CSS做到这一点?

4 个答案:

答案 0 :(得分:4)

我知道您在问一个纯CSS解决方案,但是由于使用Javascript如此容易实现,因此我也想添加该解决方案。对于将来可以选择使用Javascript的读者来说,也可能会有帮助。

此外,在目前时间,我相信不能仅使用CSS来完成此操作。使用Javascript的另一个好处是元素的源顺序与其视觉顺序保持一致,这有助于大量的可访问性。

function reverseChildren(el) {
  [...el.children].reverse().forEach(child => el.appendChild(child));
}
div > div {
  text-align: center;
  color: white;
  background: black;
  border: 1px solid white;
}

div.row {
  display: flex;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="row" id="row">
  <div class="col-4">1</div>
  <div class="col-4">2</div>
  <div class="col-4">3</div>
  <div class="col-4">4</div>
  <div class="col-4">5</div>
  <div class="col-4">6</div>
  <div class="col-4">7</div>
</div>

<button type="button" onclick="reverseChildren(document.getElementById('row'))">Reverse order</button>

因此,让我们尝试一下如何处理1000 div:

const row = document.querySelector('.row');
const fragment = new DocumentFragment();

for (let i = 1; i<=1000; i++) {
  const div = document.createElement('div');
  div.className = "col-4";
  div.textContent = i;
  fragment.appendChild(div);
}

row.appendChild(fragment);

function reverseChildren(el) {
  const fragment = new DocumentFragment();
  [...el.children].reverse().forEach(child => {
    el.removeChild(child);
    fragment.appendChild(child);
  })
  el.appendChild(fragment);
}
div > div {
  text-align: center;
  color: white;
  background: black;
  border: 1px solid white;
}

div.row {
  display: flex;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<button type="button" onclick="reverseChildren(document.getElementById('row'))">Reverse order</button>
<div class="row" id="row"></div>
<button type="button" onclick="reverseChildren(document.getElementById('row'))">Reverse order</button>

在第二个示例中,我修改了reverseChildren函数以使其与DocumentFragment一起使用,这旨在提高性能。

答案 1 :(得分:0)

使用引导程序4时,可以使用order类来指定div的顺序。

示例:

div > div {
  text-align: center;
  color: white;
  background: black;
  border: 1px solid white;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="row">
  <div class="col-4 order-7">1</div>
  <div class="col-4 order-6">2</div>
  <div class="col-4 order-5">3</div>
  <div class="col-4 order-4">4</div>
  <div class="col-4 order-3">5</div>
  <div class="col-4 order-2">6</div>
  <div class="col-4 order-1">7</div>
</div>

答案 2 :(得分:0)

我认为做到这一点的纯CSS方法是:

div.row {
    display: flex;
    flex-direction: column-reverse;
}

答案 3 :(得分:-1)

还有另一个css属性,可以按您想要的方式对元素进行排序,即:

https://www.w3schools.com/cssref/css3_pr_order.asp

但是,我猜只有在知道确切数目的节点的情况下,这才是一个好选择。