Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Обернуть каждые 5 div (https://javascript.ru/forum/jquery/31784-obernut-kazhdye-5-div.html)

рони 01.09.2016 12:48

обернуть элементы на js
 
emptyindorill,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
  <style type="text/css">
li{
  padding:5px;
}
.nav-column {
  border: 1px solid Blue;
  margin: 3px;
  list-style: none;
}
  </style>
  <title></title>
<script>
document.addEventListener('DOMContentLoaded', fn, false);
function wrapEach(element, wrapper, countToWrap, cls){
 var container;
 [].forEach.call( document.querySelectorAll(element), function(el,i) {
   i % countToWrap || (container = document.createElement(wrapper),
   cls && container.classList.add(cls),
   el.parentNode.replaceChild(container,el));
   container.appendChild(el);
 });
}
function fn()
{
  wrapEach('.dropdown_2columns .level2>li','div',5,'nav-column');
}

</script>
</head>
<body>
<div class="dropdown_2columns">
    <ul class="level2">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
        <li>11</li>
        <li>12</li>
        <li>13</li>
        <li>14</li>
        <li>15</li>
        <li>16</li>
        <li>17</li>
    </ul>
</div>

</body>

</html>

dd_smol 01.09.2016 17:23

Еще как вариант. :)

jQuery(function ( $ ) {
	var $div = $('div');

	$div.each(function (i, el) {
		if ( i % 5 === 0 ) {
			$div.slice(i, i+5).wrapAll('<div />');
		}
	});
});

emptyindorill 15.02.2017 09:03

Цитата:

Сообщение от dd_smol (Сообщение 427417)
Еще как вариант. :)

jQuery(function ( $ ) {
	var $div = $('div');

	$div.each(function (i, el) {
		if ( i % 5 === 0 ) {
			$div.slice(i, i+5).wrapAll('<div />');
		}
	});
});

Спасибо большое.

Уже сколько времени прошло, и снова я в этой теме)
Помогло.


Часовой пояс GMT +3, время: 23:35.