Based on this SO I try to group divs by its div attribute "messsage-id".
So all divs with the same "message-id" should be wrapped by a div with class name group.
<div class="message" message-id="1">a</div>
<div class="message" message-id="1">a</div>
<div class="message" message-id="2">a</div>
<div class="message" message-id="2">a</div>
<div class="message" message-id="3">a</div>
But I dont get it to work with attributes instead of class names. Here is what I tried:
var groups = {};
$('.day').each(function () {
var className = this.getAttribute("message-id").match(/(dayd+)/)[1],
$group = groups[className];
if (!$group) {
$group = $('<div />', {
'class': 'group'
}).insertAfter(this);
groups[className] = $group;
}
$group.append(this);
})
Here is the Fiddle
question from:
https://stackoverflow.com/questions/65923907/group-divs-by-attribute 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…