te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - how to add a wrapper div to an inner div - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add a wrapper div to an inner div - Stack Overflow

programmeradmin4浏览0评论

I have a an html code like this

<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. So that the code bees 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.. :)

I have a an html code like this

<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. So that the code bees 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.. :)

Share Improve this question asked Apr 16, 2011 at 5:50 ghostCoderghostCoder 7,6559 gold badges53 silver badges74 bronze badges 2
  • 1 I'm curious - what's the use case for this? Are you trying to do some drag/drop functionality? – Demian Brecht Commented Apr 16, 2011 at 5:52
  • actually what i need to do is to delete the div with class foo as i am storing the html code in the db, and replace it with a custom tag <quirk class = foo> as i save it. the code in the div with class foo is regenerated each time the whole code is loaded from the db. so when the code is loaded all the quirk custom tags are replaced with divs and the code inside them regenerated. so, i as thinking, i'll add the extra div wrapper, change the html inside it using that div, and then remove the wrapper div i added – ghostCoder Commented Apr 16, 2011 at 5:57
Add a ment  | 

3 Answers 3

Reset to default 7

Using POJS is pretty simple:

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 (e.g. don't give it a TR or TD or such).

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.

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. 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. It might work, but seems prone to error to me. So here's a better solution, tested in 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)
  }
}
$('.foo').wrap('<div id="temp"/>');

See $.wrap()

Note that if there are more elements than 1 wrapped, you got more elements with the ID "temp"

var wrapper = document.createelement('div'); 
var myDiv = document.getelementById('myDiv'); 
wrapper.appendChild(myDiv.cloneNode(true)); 
myDiv.parentNode.replaceChild(wrapper, myDiv);
发布评论

评论列表(0)

  1. 暂无评论