简体   繁体   English

添加Div标签以包裹LI元素

[英]Adding a Div tag to wrap around LI elements

I would like to dynamically add a pair of Div tags and a class to wrap a set of elements in the DOM. 我想动态添加一对Div标签和一个类来包装DOM中的一组元素。 How would I go about doing this? 我该怎么做呢?

Html HTML

<div class="right-bottom"> <!-- this is the div tag I would like to add to wrap the content below-->
    <li class="">
        <a href="">Diffusion Tensor Imaging</a>
        <article class="article-body" data-asynchtml-target="">
            <div class="loader"></div>
        </article>
     </li>

</div> 

JavaScript JavaScript的

<script type="text/javascript">
    var div = document.createElement('div');
</script>

You can use .wrap() with appropriate selector to target li elements. 您可以使用带有适当选择器的.wrap()来定位li元素。 something like this: 这样的事情:

$('.article-body').closest('li').wrap('<div class="right-bottom"></div>')

See Documentation . 文档

Here is an example: get parent, detach element, append wrap to parent, append element to wrap. 下面是一个示例:获取parent,detach元素,将wrap包装到parent,追加要包装的元素。

function wrapElement(element) {
    var parent = element.parentNode,
        wrap   = document.createElement('div');

    wrap.classList.add('wrap');
    element.remove();
    parent.appendChild(wrap);
    wrap.appendChild(element);
}

http://jsbin.com/hagovune/1/edit?html,css,js,output http://jsbin.com/hagovune/1/edit?html,css,js,output

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

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