Wrap every 3 divs in a div

前端 未结 6 1862
执念已碎
执念已碎 2020-11-22 09:03

Is it possible to use nth-child selectors to wrap 3 divs using .wrapAll? I can\'t seem to work out the correct equation.

so...



        
6条回答
  •  隐瞒了意图╮
    2020-11-22 09:51

    The Plugin

    $(function() {
        $.fn.EveryWhat = function(arg1) {
            var arr = [];
            if($.isNumeric(arg1)) {
                $.each(this, function(idx, item) {
                    var newNum = idx + 1;
                    if(newNum%arg1 == 0)
                    arr.push(item);
                });
            }
            return this.pushStack(arr, "EveryWhat", "");
        }
    });
    

    How to use it.

    Call EveryWhat() on the element and put in a number for every element you would like to collect.

    $("div").EveryWhat(2).wrapInner('
    ');

    wrapinner's quotes should have a properly formatted

    with a class and closing tag. Stackoverflow prevents me from showing what that looks like but here is a link of a self closing div.

    What it should look like

    That will wrap every other number that you specified. I'm using jquery 1.8.2. so remember use selector call EveryWhat(3) and a number for every time. Of course putting it at the bottom of the page or wrapping it in a

    $(document).ready(function() {  
        //place above code here
    });
    

    You could use every nth and then .wrapInner('

    ') for the same results.

提交回复
热议问题