我有一个包含用户名列的表。在表格旁边,我想单独列出所有用户名,并计算它们在表格中出现的次数。
问问题
100 次
1 回答
0
HTML
<div class="username">jsmith</div>
<div class="username">jsmith</div>
<div class="username">bruth</div>
<div class="username">bruth</div>
<div class="username">jsmith</div>
<div class="username">jsmith</div>
<div class="username">bdole</div>
<div class="username">bdole</div>
<br />
<div id="result"></div>
Javascript
var divs = []
$('.username').each(function() {
var uid = $(this).html();
if ($.inArray(uid, divs) < 0) {
divs.push(uid);
}
}).promise().done(function() {
$.each(divs, function(index, value) {
var sel = '.username:contains("' + value + '")';
var n = $(sel).length;
$('#result').append('<div>' + value + ' x' + n + '</div>');
});
});
于 2013-09-12T23:13:59.713 回答