typescript 在类的每两个子元素中添加样式属性,每个类都应该独立运行,包括它们的最后一个子元素(包含if语句)

sd2nnvve  于 2023-01-18  发布在  TypeScript
关注(0)|答案(1)|浏览(92)
    • 编辑**请检查答案,使用javascript的目的是不好的。我会离开这个问题,因为它可能会纠正别人太多。

我的目标是每个. ulInCollapseli都应该被独立计算。如果我给不同的类(或id)命名,比如.ulInCollapse1.ulInCollapse2等等,我确实实现了我的目标,但是我知道这不是正确的方式。我还修改了我的js代码以进行迭代;

const ulInCollapse = document.querySelectorAll(".ulInCollapse");
console.log(ulInCollapse)
ulInCollapse.forEach(function (ulInCollapse) {
  var ulInCollapseLiCount = ulInCollapse.length;
  var ulInCollapseLiCountChild = document.querySelector(".ulInCollapse li:last-child");
  if (ulInCollapseLiCount % 2 != 0) {
    ulInCollapseLiCountChild.style.paddingTop = "15px";
  } else {
    ulInCollapseLiCountChild.style.paddingTop = "0px";
  }});

我不知道该怎么办,我希望我能把问题解释清楚。
x一个一个一个一个x一个一个二个一个x一个一个三个一个

wwwo4jvm

wwwo4jvm1#

也许你的意思是如何通过javascript正确设置文档中每个列表的最后一个列表项的填充。
这应该可以达到目的,但我不确定这是不是达到目的的最佳策略,我的意思是,使用不同样式的不同方法会比设置填充的方式更优雅。
我只处理了代码的javascript部分,在css规则中,我添加了一个红色的左边框来查看选择器何时真正匹配,并为每个列表项添加了一个轮廓,以便很明显应用了正确的填充。

const ulInCollapse = document.querySelectorAll(".ulInCollapse");

//for each .ulInCollapse
ulInCollapse.forEach( ul => {
  //the number of li elements contained in the current ul
  const liCount = ul.querySelectorAll(':scope > li')?.length;
  //15px if liCount is odd, otherwise 0
  const padding = (liCount % 2 !== 0) ? '15px' : '0';
  //sets the the padding-top of the last li in this ul
  ul.querySelector(':scope > li:last-child').style.paddingTop = padding;
})
/* here I am trying to add padding to every 2 li starting from the first */
.ulInCollapse li:nth-child(2n+1):not(:last-child) {
    padding: 15px 0px;
    border-left: solid red;
}

/* I am using js to add padding-top in this selector */
.ulInCollapse li:last-child {
    padding-top: 0px;
}

li {
  outline: solid;
}
<button class="collapsible">Basit Zamanlar (The
      Simple Tenses)</button>
    <div class="content">
      <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
        <!-- question: add padding to every two element, how??? -->
        <li><a href="#">Simple Present Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
      </ul>

    </div>
    <button class="collapsible">Geçmiş Zamanalar (The Past Tenses)</button>
    <div class="content">
      <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
        <!-- question: add padding to every two element, how??? -->
        <li><a href="#">Simple Present Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
      </ul>
    </div>
    <button class="collapsible">Gelecek Zamanlar (The Future Tenses)</button>
    <div class="content">
      <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
        <!-- question: add padding to every two element, how??? -->
        <li><a href="#">Simple Present Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
        <li><a href="#">Simple Perfect Tense</a></li>
      </ul>
    </div>

这是一个更好的解决Flex容器中元素样式的方法。在容器中,我使用gap来选择元素之间的距离,并指定margin: 10em 0来设置宏兄弟之间的间距。使用填充来分隔项不是一个好主意,因为填充是用来分隔内容*内部而不是外部。
仅在容器上执行此操作就足以正确设置其项的布局:

.ulInCollapse {
  display: flex;
  flex-direction: column;
  gap: 14px;    
  margin: 10px 0;
}
.ulInCollapse {
  list-style-type: none;
  margin: 0;
  padding: 0;
  
  display: flex;
  flex-direction: column;
  gap: 14px;    
  margin: 10px 0;
}

ul.ulInCollapse li {
  /*
    !!!!
    again this border is to highlight the elegance of this solution
    if you will set the borders to your solution that used paddings you'll see incongruent sizing
  */
  border: solid 1px;
}

ul.ulInCollapse li a {
  color: rgb(0, 132, 255);
  font-size: large;
  text-decoration: none;
}
<button class="collapsible">Basit Zamanlar (The Simple Tenses)</button>
<div class="content">
  <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
    <li><a href="#">Simple Present Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
  </ul>
</div>

<button class="collapsible">Geçmiş Zamanalar (The Past Tenses)</button>
<div class="content">
  <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
    <li><a href="#">Simple Present Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
  </ul>
</div>

<button class="collapsible">Gelecek Zamanlar (The Future Tenses)</button>
<div class="content">
  <ul class="ulInCollapse" style="list-style-type: none; padding: 0%;">
    <li><a href="#">Simple Present Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
    <li><a href="#">Simple Perfect Tense</a></li>
  </ul>
</div>

相关问题