2012-04-17 51 views
2

容器div.example可以有不同的1級子元素(部分DIVUL導航 ...)。這些元素的數量和類型可能會有所不同。查找DOM元素出現最

我必須找到發生最多的直接孩子的類型(例如div)。 什麼是簡單的jQuery或JavaScript解決方案?

jQuery 1.7.1可用,但它也應該在IE <(array.filter)中工作。

編輯:使用.children()謝謝@Jasper,@Vega和@Robin Maben :)

回答

3

迭代通過兒童和日誌您發現element.tagName S上的號碼:

//create object to store data 
var tags = {}; 

//iterate through the children 
$.each($('#parent').children(), function() { 

    //get the type of tag we are looking-at 
    var name = this.tagName.toLowerCase(); 

    //if we haven't logged this type of tag yet, initialize it in the `tags` object 
    if (typeof tags[name] == 'undefined') { 
     tags[name] = 0; 
    } 

    //and increment the count for this tag 
    tags[name]++; 
}); 

現在tags對象保存作爲#parent元素的子節點出現的每種標籤類型的編號。

這裏是一個演示:http://jsfiddle.net/ZRjtp/(看你的對象控制檯)

然後找到發生的最你可以這樣做標籤:

var most_used = { 
     count : 0, 
     tag : '' 
    }; 

$.each(tags, function (key, val) { 
    if (val > most_used.count) { 
     most_used.count = val; 
     most_used.tag = key; 
    } 
}); 

most_used對象現在持有的標籤使用次數最多,使用次數也最多。

這裏是一個演示:http://jsfiddle.net/ZRjtp/1/

+1

而不是'$。每個($( '#父')。子女()',你可以做'$( '#parent')。children()。each('。 – 2012-04-17 19:53:44

+0

@Rocket是的,它可能看起來有趣,但'$ .each()'更快,所以爲什麼不使用它?:)下面是一個JSPerf來展示它們之間的區別:http://jsperf.com/jquery-each-vs-for-loops/7 – Jasper 2012-04-17 20:00:51

+0

這個測試是針對數組的,當處理DOM元素(jQuery選擇器)時,'.each'更好。http:// jsperf .com/jquery-each-vs-jquery-each – 2012-04-17 20:10:12

2

編輯:我覺得像下面一個jQuery功能應該比較有用..

DEMO

$.fn.theMostChild = function() { 
    var childs = {}; 
    $(this).children().each(function() { 
     if (childs.hasOwnProperty(this.nodeName)) { 
      childs[this.nodeName] += 1; 
     } else { 
      childs[this.nodeName] = 1; 
     } 
    }); 
    var maxNode = '', maxNodeCount = 0; 
    for (nodeName in childs) { 
     if (childs[nodeName] > maxNodeCount) { 
      maxNode = nodeName; 
      maxNodeCount = childs[nodeName]; 
     } 
    } 
    return $(maxNode); 
} 

然後你就可以了,

$('div.example').theMostChild().css('color', 'red'); 

下面的函數應該給你計算子元素的數量,從中你可以得到最大數量。見下文, DEMO

$(function() { 
    var childs = {}; 
    $('div.example').children().each(function() { 
     if (childs.hasOwnProperty(this.nodeName)) { 
      childs[this.nodeName] += 1; 
     } else { 
      childs[this.nodeName] = 1; 
     } 
    }); 

    for (i in childs) { 
     console.log(i + ': ' + childs[i]); 
    } 
}); 
+0

只是一個簡單的問題,在最後()不應該是返回值是$(maxNode)而不是$(nodeName) – 2012-04-17 20:11:24

+0

@aawww true。傻我..謝謝你糾正 – 2012-04-17 20:11:58

+0

@Vega:非常感謝你的回答。在兩個相似的答案中總是很難選擇,特別是如果他們的發佈時間之間只有3分鐘。 – 2012-04-18 03:45:56

1

這也不是沒有可能對預期的類型的子節點的一些信息。

編輯:可能爲Jasper指出我們不需要事先知道標籤名稱。如果您只在特定的一組選擇器內查找,以下工作。

var selectorArray = ['div', 'span', 'p',........] 

var matches = $(div).children(selectorArray.join());  
var max = 0, result = [];  
$.each(selectorArray, function(i, selector){ 

    var l = matches.filter(selector).length; 
    if(l > max){ 
    max = l; 
    result[max] = selector; 
    } 

}); 

result[max]給你的標籤名稱和max給你的出現計數

+2

'tagName'應該把這個把戲對嗎? https://developer.mozilla.org/en/DOM/element.tagName – Jasper 2012-04-17 19:50:14

+0

@Jasper:是的,你是對的。忽略了。更新了我的答案。 – 2012-04-17 19:57:05

+0

@Robin:非常感謝你的回答:)你知道,如果較老的IE與_matches.filter(選擇器).length_有問題嗎? [ECMAScript 5兼容表](http://kangax.github.com/es5-compat-table/#showold)表示在IE6/7/8中不支持Array.filter。 – 2012-04-18 03:39:50