首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找一个最常见的类

找一个最常见的类
EN

Stack Overflow用户
提问于 2015-03-23 03:34:58
回答 5查看 147关注 0票数 1

我很抱歉这个问题的形式。我知道应该包括一些代码,但我甚至不知道从哪里开始。我需要找到在div中最常见的类,其中包含一个特定的符号。问题是找出最常见的问题。

假设有一些div

代码语言:javascript
复制
<div class="mainclass subclass-1 subclass1"></div>
<div class="mainclass subclass-1 subclass3"></div>
<div class="mainclass subclass-2 subclass3"></div>
<div class="mainclass subclass-1 subclass2"></div>
<div class="mainclass subclass-1 subclass1"></div>

如何找到子类的总数-1(在本例中?)。类是动态生成的,我从不知道每次哪个类最常见。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-03-23 03:59:16

您可以从下面的代码中找到类的完整列表及其计数。

代码语言:javascript
复制
var classes = new Object();

$("*").each(function() {
    if (this.className.length > 0)
    {
        var arrTmp = this.className.split(" ");
        for (var i=0; i<arrTmp.length; i++)
        {
            if ($.trim(arrTmp[i]).length > 0)
            {
                if (classes[arrTmp[i]])
                    classes[arrTmp[i]] = classes[arrTmp[i]] + 1;
                else
                    classes[arrTmp[i]] = 1;
            }
        }
    }    
});

var sortable = [];
for (var arr in classes)
      sortable.push([arr, classes[arr]])
sortable.sort(function(a, b) {return b[1] - a[1]});

alert(sortable);

在JsFiddle:https://jsfiddle.net/qsfdm286/上的测试

票数 0
EN

Stack Overflow用户

发布于 2015-03-23 03:42:06

你可以的

代码语言:javascript
复制
var counter = {}, //to store the count of occurences temporarely
max; //the most common class
//iterate over te main class
$('.mainclass').each(function () {
    //find the subclass-* value
    var cs = this.className.match(/subclass-\d+/)[0];
    counter[cs] = counter[cs] || 0;
    //increment the count of occurrence
    counter[cs]++;
    if (counter[cs] > (counter[max] || 0)) {
        //if the current one is more then change the max to current class
        max = cs;
    }
});
console.log(max)

演示:小提琴

如果你也想考虑subclass1的话

代码语言:javascript
复制
var counter = {}, //to store the count of occurences temporarely
max; //the most common class
//iterate over te main class
$('.mainclass').each(function () {
    //find the subclass-* value
    var parts = this.className.match(/subclass-?\d+/g);
    $.each(parts, function (i, cs) {
        counter[cs] = counter[cs] || 0;
        //increment the count of occurrence
        counter[cs]++;
        if (counter[cs] > (counter[max] || 0)) {
            //if the current one is more then change the max to current class
            max = cs;
        }
    })
});
console.log(max, counter)

演示:小提琴

票数 1
EN

Stack Overflow用户

发布于 2015-03-23 03:40:42

当您了解该类时,您可以按以下方式查找该类的用法:

代码语言:javascript
复制
var classList = document.getElementById('divId').className.split(/\s+/);
for (var i = 0; i < classList.length; i++) {
   var numItems = $('.'+classList[i]).length;
   console.log('Usage of class '  classList[i] 'is ' numItems ) 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29202888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档