简体   繁体   English

获取具有指定值的JS中的元素计数(无JQUERY)

[英]Get the Count of Element in JS which has the specified Value (Without JQUERY)

I have an requirement in JS where, i want to get the count of li element which has the same inner value. 我在JS中有一个要求,我想获取具有相同内部值的li元素的计数。 What is the best way to do it ? 最好的方法是什么? I know we can achieve this in Jquery easily using find and contains but i don't want to use jquery. 我知道我们可以使用find和contains在Jquery中轻松实现此目的,但我不想使用jquery。

I want the length of li elements has the Same Value. 我希望li元素的长度具有相同的值。

For Eg: Say i want to find out how many LI has the value 'A'. 例如:说我想找出多少个LI的值为“ A”。

Below is the JS i have tried, which i think is not the best cos if i have say around 10,000 LI then i will have to loop through all the elements get their values and check if its what i want or no, which will surely hit the performance. 下面是我尝试过的JS,如果我说过10,000 LI,我认为这不是最好的cos,那么我将不得不遍历所有元素以获取其值,并检查其是否想要或不想要,这肯定会达到目标表演。

Note : LI element is added runtime with their Value. 注意:LI元素及其值添加到运行时。

HTML HTML

<ul class="s_1" id="ULE">
    <li class="r1">A</li>
    <li class="r1">A</li>    
    <li class="r1">B</li>
    <li class="r1">A</li>  
</ul>  

JS JS

var LI = document.getElementsByClassName('r1');
var cnt = 0;
for(var i=0;i<LI.length;i+=1){
    if(LI[i].innerHTML == 'A'){
        cnt += 1;
    }
}

if(cnt === 4) 
      alert('working good!!');

JS Fiddle JS小提琴

I don't think there is a better way to improve the performance. 我认为没有更好的方法来提高性能。 If you have nothing but the DOM to work with (eg if the data is from user input), and not the underlying data structure from which you created the data, AFAICT there is no other structure to collect all of the elements than into an array-type structure, which will then require O(n) time to check every element. 如果您只需要使用DOM(例如,数据来自用户输入),而不是用于创建数据的基础数据结构,则AFAICT除了收集到数组中之外,没有其他结构可以收集所有元素类型的结构,这将需要O(n)时间来检查每个元素。

Rather than have a count target, which is therefore dependent on the amount of list elements, try instead a function to handle the data, which increases the convenience somewhat: 与其有一个计数目标,它因此取决于列表元素的数量,而不是尝试一个处理数据的函数,这在某种程度上增加了便利性:

function isEveryElementEqual(nodeList) {
    val = nodeList[0].innerHTML;
    for (var i=1; i<nodeList.length; i++) {
        if (nodeList[i].innerHTML !== val) return false;
    }
    return true;
}

var LI = document.getElementsByClassName('r1');

console.log(isEveryElementEqual(LI)); // Returns false with the above HTML

You don't have to search all <li> in the document. 您不必搜索文档中的所有<li> getElementsByClassName can be applied to an element, it will then only search within that element. 可以将getElementsByClassName应用于元素,然后仅在该元素内搜索。 So you can do: 因此,您可以执行以下操作:

var LI = document.getElementById('ULE').getElementsByClassName('r1');

DEMO DEMO

You can also simplify this with querySelectorAll : 您还可以使用querySelectorAll简化此操作:

var LI = docuement.querySelectorAll('#ULE .r1');

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

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