最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Get element by PART of class name [JavaScript] - Stack Overflow

programmeradmin1浏览0评论

I'm trying to make a chrome extension. A part of the code (early on in the making of this extension) involves fetching several elements by their class name. There are several elements whos class name all look like this: "scollerItem [AlotOfRandomCharacters]". So I'm trying to list all classNames that start with "scrollerItem", but i'm not quite sure how to go about it.

So here's my code so far:

function initRE(){
    var matchingItems = [];
    var allElements = document.getElementsByTagName("div");
    for(i=0; i < allElements.length; i++)
    {
        if ( allElements [i].className == "scrollerItem" && "*" )
        {
            matchingItems.push(allElements[i]);
        }
    }
    alert(matchingItems[0]);
}

allElements is listed in the breakpoint watchlist as "HTMLCollection(623)" (roughly), but nothing is forwarded to the "matchingItems" array. I've tried [i].className.contains and .includes

Direct copy of one of the HTML Elements in question:

<div class="scrollerItem s1d8yj03-2 ebdCEL Post t3_agnhuk  s1ukwo15-0 RqhAo" id="t3_agnhuk" tabindex="-1">

I'm trying to make a chrome extension. A part of the code (early on in the making of this extension) involves fetching several elements by their class name. There are several elements whos class name all look like this: "scollerItem [AlotOfRandomCharacters]". So I'm trying to list all classNames that start with "scrollerItem", but i'm not quite sure how to go about it.

So here's my code so far:

function initRE(){
    var matchingItems = [];
    var allElements = document.getElementsByTagName("div");
    for(i=0; i < allElements.length; i++)
    {
        if ( allElements [i].className == "scrollerItem" && "*" )
        {
            matchingItems.push(allElements[i]);
        }
    }
    alert(matchingItems[0]);
}

allElements is listed in the breakpoint watchlist as "HTMLCollection(623)" (roughly), but nothing is forwarded to the "matchingItems" array. I've tried [i].className.contains and .includes

Direct copy of one of the HTML Elements in question:

<div class="scrollerItem s1d8yj03-2 ebdCEL Post t3_agnhuk  s1ukwo15-0 RqhAo" id="t3_agnhuk" tabindex="-1">
Share Improve this question edited Jan 16, 2019 at 21:52 celicoo 1,1188 silver badges18 bronze badges asked Jan 16, 2019 at 21:15 svemalsvemal 871 gold badge1 silver badge6 bronze badges 1
  • Why not document.getElementsByClassName('scrollerItem')? – connexo Commented Jan 16, 2019 at 22:00
Add a comment  | 

3 Answers 3

Reset to default 10

You can try to use Document.querySelectorAll() with a CSS selector matching all classes starting with your target string.

let elems = document.querySelectorAll("div[class^='scrollerItem'], div[class*=' scrollerItem]");
let classes = Array.from(elems).map(e => Array.from(e.classList)).reduce((arr, res) => {
    res = res.concat(arr);
    return res;
}, []).filter(cls => cls.startsWith('scrollerItem'))

Additional explanation of CSS selector syntax: Is there a CSS selector by class prefix?

Since the class scrollerItem exists on the element, you can use document.querySelectorAll() with the .scrollerItem as the query:

const result = document.querySelectorAll('.scrollerItem');

console.log(Array.from(result));
<div class="scrollerItem s1d8yj03-2 ebdCEL Post t3_agnhuk  s1ukwo15-0 RqhAo" id="t3_agnhuk" tabindex="-1">

Use classList not className:

if (allElements[i].classList.contains("scrollerItem") {...}
发布评论

评论列表(0)

  1. 暂无评论