daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Aug 14th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. div manipulation [closed]
  2. <div class="out">
  3. out asdaasd
  4. <div class="in"> in </div>
  5. out
  6. </div>
  7.    
  8. <div class="out"> out asdaasd <div>
  9. <div class="in"> in </div>
  10. <div class="out"> out </div>
  11.    
  12. var outers = document.getElementsByClassName("out");
  13.  
  14. [].forEach.call(outers, function(outer) {
  15.     var inner = outer.getElementsByClassName("in")[0];
  16.     var children = outer.childNodes;
  17.  
  18.     var flag = true;
  19.     var before = document.createElement("div");
  20.     var after = document.createElement("div");
  21.     before.classList.add("out");
  22.     after.classList.add("out");
  23.  
  24.     [].slice.call(children).forEach(function(node) {
  25.         if (node == inner) {
  26.             return flag = false;
  27.         }
  28.         if (flag) {
  29.             before.appendChild(node);
  30.         } else {
  31.             after.appendChild(node);
  32.         }
  33.     });
  34.     var frag = document.createDocumentFragment();
  35.     frag.appendChild(before);
  36.     frag.appendChild(inner);
  37.     frag.appendChild(after);
  38.     outer.parentNode.replaceChild(frag, outer);
  39. });
  40.    
  41. <div class="out">
  42. out asdaasd
  43. <div class="in"> in </div>
  44. out
  45. </div>
  46.  
  47. <div id="xyz">
  48. </div>
  49.  
  50. $('.out').contents().each(function() {        
  51.     if(this.nodeType == 3) {
  52.         $('#xyz').append("<div class='out'>"+$(this).text()+"</div>");
  53.     }
  54.     else {
  55.         $('#xyz').append(this);
  56.     }
  57. });
  58.    
  59. var a = $('<div class="out"> out asdaasd<div class="in"> in </div>out</div>'),
  60.   tmp = $("<div/>");
  61.  
  62. a.contents().each(function() {
  63. if(this.nodeType == 1)
  64.   tmp.append(this);
  65. else
  66.   tmp.append($(this).wrap("<div class='out' />").parent());
  67. });
  68. tmp.html()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top