简体   繁体   English

如何获取填充的li元素之间的空li元素的长度?

[英]How can i get the length of empty li elements between to populated li elements?

I have multiple <li> elements and they will be populated randomly after loading. 我有多个<li>元素,加载后将随机填充它们。 For example if I have twelve <li> elements and only two of them are populated, how can I get the number of empty <li> elements and the populated ones? 例如,如果我有十二个<li>元素并且仅填充了其中两个,那么如何获取空的<li>元素和填充的元素的数量?

This is a test markup, my problem is how can I get the length between populated <li> : 这是一个测试标记,我的问题是如何获取填充的<li>之间的长度:

<ul>
    <li></li>
    <li><span>text</span></li>
    <li></li>
    <li></li>
    <li></li>
    <li><span>text</span></li>
</ul>

Im not sure, that I understand what you want but here is an example what I think you want. 我不确定,我了解您想要什么,但这是我认为您想要的示例。

    <script>

    function getItems() {
       var count = [0,0];

       $("li").each(function() {
       if($(this).text().length != 0)
          count[0]++;
       else
          count[1]++;
       });
       return count;
    }

    var items = getItems();
    // items[0] should return 4
    // items[1] should return 2

    </script>

I hope this can help you. 希望对您有所帮助。

Hope can help you. 希望可以帮到您。

function getItems() {
    let indexNotEmpty = [];
    $('ul > li').each(function(i, item) {

        if(item.innerText != ''){
            indexNotEmpty.push(i);
        };
    });

  return indexNotEmpty[indexNotEmpty.length-1]-indexNotEmpty[0]-1;
}

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

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