我想选择每 4 个 divclass "contents"
并将其包装在另一个使用动态创建的新 div 中Jquery
。我的HTML如下;
<body>
<div id="postings">
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
</div>
</body>
我想
<body>
<div id="postings">
<div id="newDivforWraping1">
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
</div>
<div id="newDivforWraping2">
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
</div>
<div id="newDivforWraping3">
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
<div class="contents"></div>
</div>
</div>
</body>
我当前的 JScript 代码如下;
(function() {
"use strict";
$(document).ready(function() {
insertdiv("newDivforWraping1",4);
insertdiv("newDivforWraping2",8);
insertdiv("newDivforWraping3",12);
});
//create div for every grouping them into three
function insertdiv(divname,n) {
$("<div/>", {class: "test",id:divname}).appendTo("#postings");
var allcontentspost = $(".contents").each();
for (var i=n-4;i<n;i++) {
allcontentspost[i].appendTo("#"+divname);
};
}
})();