简体   繁体   English

如何将包装器div添加到内部div

[英]how to add a wrapper div to an inner div

I have a an html code like this 我有一个这样的html代码

<div>
<p> blah blah </p>
<div class = foo>

//some code

</div>
//some text
</div>

What i want to do by javascript is to add a wrapper div to the inner div with class foo. 我想通过javascript做的是使用foo类将包装器div添加到内部div中。 So that the code becomes something like 这样代码就变得像

<div>
<p> blah blah </p>
<div id = temp>
<div class = foo>

//some code
</div>
</div>
//some text
</div>

Please tell me how to do something like this. 请告诉我如何做这样的事情。 Non jquery solutions would be more helpful.. :) 非jquery解决方案将更有用.. :)

Using POJS is pretty simple: 使用POJS非常简单:

function divWrapper(el, id) {
  var d = document.createElement('div');
  d.id = id;
  d.appendChild(el);
  return d;
}

Make sure you pass it something that can be wrapped in a div (eg don't give it a TR or TD or such). 确保您传递的东西可以包装在div中(例如,不要给它TR或TD等)。

You'll need some helper functions, I'm not going to post a getElementsByClassName function here, there are plenty on the web to choose from, a good one should first try qSA, then DOM method, then custom function. 您将需要一些辅助函数,在这里我不会发布getElementsByClassName函数,网络上有很多可供选择,一个好的函数应该首先尝试qSA,然后是DOM方法,然后是自定义函数。

Assuming you have one, consider something like: 假设您有一个,请考虑以下内容:

function wrappByClass(className) {
  var el, elements = getElementsByClassName(className);

  for (var i = elements.length; i--;) {
    el = elements[i];
    el.parentNode.replaceChild(divWrapper(el, 'foo' + i), el);
  }
}

Edit 编辑

On reflection, I prefer the following method. 考虑一下,我更喜欢以下方法。 It inserts the wrapper div into the DOM first, then moves the element to be wrapped into it. 它将包装器div首先插入DOM,然后将要包装的元素移动到DOM中。 The above seems to move the element out of the DOM, then wants to use its position in the DOM to insert the new node. 上面的方法似乎是将元素移出DOM,然后要使用其在DOM中的位置插入新节点。 It might work, but seems prone to error to me. 它可能有效,但对我来说似乎容易出错。 So here's a better solution, tested in Safari: 因此,这是在Safari中测试过的更好的解决方案:

// Quick implementation of getElementsByClassName, just for prototypeing
function getByClassName(className, root) {
  var root = root || document;
  var elements = root.getElementsByTagName('*');
  var result = [];
  var classRe = new RegExp('(^|\\s)' + className + '(\\s|$)');

  for (var i=0, iLen=elements.length; i<iLen; i++) {

    if (classRe.test(elements[i].className)) {
      result.push(elements[i]);
    }
  }
  return result;
}

var divWrapper = (function() {
  var div = document.createElement('div');

  return function(el, id) {
    var oDiv = div.cloneNode(false);
    oDiv.id = id;
    el.parentNode.insertBefore(oDiv, el);
    oDiv.appendChild(el);
  }
}());

function wrapByClassName(className) {
  var els = getByClassName(className);
  var i = els.length;
  while (i--) {
    divWrapper(els[i], 'foo' + i)
  }
}
var wrapper = document.createelement('div'); 
var myDiv = document.getelementById('myDiv'); 
wrapper.appendChild(myDiv.cloneNode(true)); 
myDiv.parentNode.replaceChild(wrapper, myDiv);
$('.foo').wrap('<div id="temp"/>');

See $.wrap() 参见$ .wrap()

Note that if there are more elements than 1 wrapped, you got more elements with the ID "temp" 请注意,如果包装的元素多于1个,则ID为“ temp”的元素会更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM