简体   繁体   中英

How to apply display: flex and flex-direction: row in JavaScript?

I want to apply display: flex and flex-direction: row with the following code:

<ul id="myList"></ul>
<script type="text/javascript">
  const listItem = document.getElementById("myList");
  const fruits = ["Banana", "Papaya", "Mango", "Lemon"];

  for (let fruit of fruits) {
    let list = document.createElement("li");
    list.textContent = fruit;
    list.classList.add("dcode");
    listItem.appendChild(list);
  }
</script>

Assign the styles to the parent element that you have selected:

 listItem.style.display = 'flex'; listItem.style.flexDirection = 'row'

You can also assign them all at once:

 listItem.style.cssText = "display: flex; flex-direction: row";

Just add a.style for the list item elements parent and assign some cssText for display. In my example I use .style.cssText

 const listItem = document.getElementById("myList"); const fruits = ["Banana", "Papaya", "Mango", "Lemon"]; for (let fruit of fruits) { let list = document.createElement("li"); list.textContent = fruit; list.classList.add("dcode"); listItem.appendChild(list); listItem.style.cssText = 'display: flex; flex-direction: row; list-style-Type: none;' }
 <ul id="myList"></ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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