Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Как обернуть каждый тег в несколько DIV содержащих по 1 атрибуту от этих самых тегов? (https://javascript.ru/forum/misc/65795-kak-obernut-kazhdyjj-teg-v-neskolko-div-soderzhashhikh-po-1-atributu-ot-ehtikh-samykh-tegov.html)

рони 08.11.2016 11:56

Zaxap,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <script>
window.addEventListener('DOMContentLoaded', function() {
[].forEach.call( document.querySelectorAll('body *'), function(el) {
   [].reduceRight.call(el.attributes,function(elem, attr) {
   var wrapper = document.createElement("div"),
   parent = elem.parentNode;
   parent.replaceChild(wrapper,elem);
   wrapper.setAttribute(attr.name, attr.value);
   el.removeAttribute(attr.name);
   wrapper.appendChild(elem);
   return wrapper
},el)
});
    });

</script>
</head>

<body>
<div id="p1-id" class="p1-class" data-attribute="data-value">
  my
  <p сlass="p2-class1 p2-class2">
    super <i class="italic"><b>paragraph</b></i>
  </p>
</div>


</body>
</html>


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