简体   繁体   English

根据特定值获取列表中的所有先前项目?

[英]Get all previous items in list based on specific value?

I have this list: 我有这个清单:

<ul id="tabs-list" class="nav nav-pills">
   <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#registry" data-toggle="tab"></a></li>
   <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#tab1" data-toggle="tab" data-question-id="1"><i class="tim-icons icon-bulb-63"></i></a></li>
   <li class="nav-item" style="width: 25%;"><a class="nav-link checked active show" href="#tab2" data-toggle="tab" data-question-id="3"><i class="tim-icons icon-bulb-63"></i></a></li>
   <li class="nav-item" style="width: 25%;"><a class="nav-link" href="#tab3" data-toggle="tab" data-question-id="2"><i class="tim-icons icon-bulb-63"></i></a></li>
</ul>

As you can see I have the third element tab#2 which has the class active . 如您所见,我有第三个元素tab#2 ,其类为active My goal is to take all the data-question-id that are located before the active item. 我的目标是获取位于active项目之前的所有data-question-id

The output should be: ["1"] because before the tab2 there is just one item with that attribute. 输出应为: ["1"]因为在tab2之前只有一个具有该属性的项。

I can easily get the active item using this code: 我可以使用以下代码轻松获取活动项目:

var active_id = Number($(".tab-pane.active").attr('data-question-id'));

But how can I get all the previous item of the active item? 但是,如何获取active项目中的所有先前项目?

Here is a way to do it with pure JS in the functional style. 这是使用功能样式的纯JS的一种方法。

You query all selectors with the attribute data-question-id . 您使用属性data-question-id查询所有选择器。 Then you map this array to an array of objects with the questions ids and a boolean indicating if these elements hold the active class. 然后,将此数组映射到具有问题ID和一个布尔值的对象数组,该布尔值指示这些元素是否包含active类。

Then you find the index of the first object with active = true and you slice the array to get all the previous questions ids. 然后,使用active = true找到第一个对象的索引,并对数组进行切片以获取所有先前的问题ID。

 const questions = Array.from(document.querySelectorAll('a[data-question-id]')) .map(elem => ({ id: elem.getAttribute('data-question-id'), active: elem.classList.contains('active') })); const result = questions .slice(0, questions.findIndex(elem => elem.active)) .map(elem => elem.id); console.log(result) 
 <ul id="tabs-list" class="nav nav-pills"> <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#registry" data-toggle="tab"></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#tab1" data-toggle="tab" data-question-id="1"><i class="tim-icons icon-bulb-63"></i></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link checked active show" href="#tab2" data-toggle="tab" data-question-id="3"><i class="tim-icons icon-bulb-63"></i></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link" href="#tab3" data-toggle="tab" data-question-id="2"><i class="tim-icons icon-bulb-63"></i></a></li> </ul> 

Something like that? 这样的事吗?

var prevs = []
$(".tab-pane.active").each(function()
{
    if ($(this).attr('data-question-id') == active_id)
    {
        // Do something with previous elements

        prevs = null; // empty array
    }
    else if (prevs)
    {
        prevs.push($(this));
    }
})

You can use some built in jQuery methods to achieve the output. 您可以使用一些内置的jQuery方法来实现输出。

.parent()

Get the parent of each element in the current set of matched elements, optionally filtered by a selector. 获取当前匹配元素集中每个元素的父元素,可以选择使用选择器进行过滤。

.prevAll()

Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector. 获取匹配元素集中每个元素的所有先前同级,可以选择由选择器过滤。

.filter()

Reduce the set of matched elements to those that match the selector or pass the function's test. 将匹配元素的集合减少到与选择器匹配或通过功能测试的元素。

.get()

Retrieve the DOM elements matched by the jQuery object 检索与jQuery对象匹配的DOM元素

and .map() like the following way: .map() ,如下所示:

 var active_id = $('.active').parent().prevAll().filter(function() { return $(this).find('a').attr('data-question-id'); }).get().map(el => $(el).find('a').attr('data-question-id')); console.log(active_id); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul id="tabs-list" class="nav nav-pills"> <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#registry" data-toggle="tab"></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link checked" href="#tab1" data-toggle="tab" data-question-id="1"><i class="tim-icons icon-bulb-63"></i></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link checked active show" href="#tab2" data-toggle="tab" data-question-id="3"><i class="tim-icons icon-bulb-63"></i></a></li> <li class="nav-item" style="width: 25%;"><a class="nav-link" href="#tab3" data-toggle="tab" data-question-id="2"><i class="tim-icons icon-bulb-63"></i></a></li> </ul> 

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

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