使用JS包装成对的< li>在divs中 [英] Using JS to wrap pairs of <li> in divs

查看:58
本文介绍了使用JS包装成对的< li>在divs中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我需要一些非常基本的JS帮助.我试图制作一个脚本,该脚本将在<ul>中的每两个列表元素周围添加一个<div>.列表元素的数量不受我的控制,每个元素的高度也不在我的控制范围内.但是我需要一种方法来确保每对(并排显示)占用相同的空间.

It seems like I need some pretty basic JS help. I an trying to make a script that will add a <div> around every two listelements in a <ul>. The amount of list elements are not under my control, neither is the height of each element. But i need a way to ensure that every pair (shown side by side) takes up the same anount of space.

希望可以为您提供帮助

谢谢.

所以来自..

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

到...

 <ul>
     <div>
        <li></li>
        <li></li>
     </div>
     <div>
        <li></li>
        <li></li>
     </div>
     <div>
        <li></li>
        <li></li>
     </div>
 </ul>

推荐答案

使用jquery,您可以尝试:

using jquery you can try:

            $(document).ready(function(){               
                var lis = $("li");
                for(var i = 0; i < lis.length; i+=2) {
                    lis.slice(i, i+2).wrapAll("<div></div>");
                }
            });

这篇关于使用JS包装成对的&lt; li&gt;在divs中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆