简体   繁体   English

如何在 JavaScript 中应用 display: flex 和 flex-direction: row?

[英]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:我想使用以下代码应用display: flexflex-direction: row

<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:将 styles 分配给您选择的父元素:

 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.只需为列表项元素父级添加 a.style 并分配一些 cssText 以供显示。 In my example I use .style.cssText在我的示例中,我使用.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>

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

相关问题 创建一个按钮以在样式的行和列之间切换 flex-direction - Create a button to toggle flex-direction between row and column for a style 你能在 CSS 和 JavaScript 中动态改变 flexbox 的 flex-direction 吗? - Can you change the flex-direction of flexbox dynamically in CSS and JavaScript? 单击按钮时更改 flex-direction - Change the flex-direction on button click 具有 flex-direction column-reverse 的 Flex 容器不会滚动到顶部 - Flex container with flex-direction column-reverse will not scroll to top 我怎样才能像 flex-direction 一样设置我的孩子的方向,但不使用 flex? - How can I set the direction of my <th> 's children like flex-direction does but without using flex? 在具有flex-direction的flexbox中,ngx-datatable不缩小:行 - Ngx-datatable not shrinking when within flexbox with flex-direction: row 如何正确获取使用flex-direction:column显示的块的实际宽度? - How to get actual width of a block displayed with flex-direction:column properly? 如何制作 flex-direction: column; 适当地。 我的代码不起作用 - How to make flex-direction: column; properly. My code doesn't work justify-content: flex-end; 和 flex-direction: 列; 没有一起工作 - justify-content: flex-end; and flex-direction: column; are not working together 在列弯曲方向内使用行弯曲方向 - use row flex direction within column flex direction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM