简体   繁体   English

通过classname的一部分查找类

[英]Find a class by a part of classname

i trie to get a full classname "i-need-this-2" from a part of known-classname "i-need-this-" of a element. 我试图从一个元素的已知类名“i-need-this-”的一部分得到一个完整的类名“i-need-this-2”。

Example

<div id="myelement" class="firstclass i-need-this-2 anotherclass"></div>

I trie to get the complete classes of the element, make a array, and search with indexof for the wanted class, but my code failed. 我试图获取元素的完整类,创建一个数组,并使用indexof搜索所需的类,但我的代码失败了。

partofclassname='i-need-this-';
let allclasses = $('#myelement').attr('class').split(' ');
let wanted_class= allclasses[allclasses.indexOf(partofclassname)];

wanted_class is undefined and indexof is -1 wanted_class是未定义的,indexof是-1

What is wrong with my code? 我的代码出了什么问题?

Thanks a lot. 非常感谢。

Your code is almost there. 你的代码几乎就在那里。 When you use split it will return an array. 当您使用split ,它将返回一个数组。 You can use filter function and in the callback use includes . 您可以使用filter功能并在回调中使用includes So this array will have those classes which includes the keyword you are looking for 所以这个数组将包含那些包含您要查找的关键字的类

 let partofclassname = 'i-need-this-'; let matchedClass = $('#myelement').attr('class').split(' ').filter(item => item.includes(partofclassname)); console.log(matchedClass) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

allclasses.indexOf(partofclassname) will check for complete string(class). allclasses.indexOf(partofclassname)将检查完整的字符串(类)。 For part you need check each element of array. 对于部分,您需要检查数组的每个元素。 Use Array.prototype.find() and check in each element of array using String.prototype.indexOf() 使用Array.prototype.find()并使用String.prototype.indexOf()检入数组的每个元素

 partofclassname='i-need-this-'; let allclasses = $('#myelement').attr('class').split(' '); console.log(allclasses) let wanted_class = allclasses.find(c => c.indexOf(partofclassname) !== -1); console.log(wanted_class) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

With RegExp : 使用RegExp

 var patt =/\\bi-need-this-[^ ]*/g ; let allclasses = document.getElementById('myelement').className ; console.log( allclasses.match(patt)[0] ) ; 
 <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

You need to do indexOf on the class name value instead of doing that in the array of those class name: 您需要对类名值执行indexOf ,而不是在这些类名的数组中执行此操作:

 let partofclassname='i-need-this-'; let allclasses = $('#myelement').attr('class').split(' '); let index = allclasses.findIndex((className) => className.indexOf(partofclassname) !== -1); let wanted_class= allclasses[index]; console.log(wanted_class); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

Because indexOf find exact match of elements. 因为indexOf找到元素的精确匹配

try filter instead. 尝试filter

 let partofclassname = 'i-need-this-'; let allclasses = $('#myelement').attr('class').split(' '); let wanted_class = allclasses.filter(e => e.includes(partofclassname)); console.log(wanted_class) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

There are multiple ways to achieve your desired classname. 有多种方法可以实现所需的类名。

 let allclasses = $('#myelement').attr('class').split(' '); let partofclassname='i-need-this-'; let wanted_class = allclasses.find(element => element.includes(partofclassname)); console.log(wanted_class); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

  • You could simply do it using a loop & includes as follows - 你可以简单地使用循环和includes如下 -

 let allclasses = $('#myelement').attr('class').split(' '); let partofclassname='i-need-this-'; let wanted_class = undefined; allclasses.forEach(element => { wanted_class = element.includes(partofclassname) ? element : wanted_class }); console.log(wanted_class) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myelement" class="firstclass i-need-this-2 anotherclass"></div> 

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

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