Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

function中参数的一点疑问

html部分如下

<div class="bd">
                        <ul>
                            <li>
                                <a class="pic" href="#"><img _src="img/wyPic1.jpg" src="images/blank.png"/></a>
                                <!--<p>李玟亲赴红十字会赈灾</p>-->
                            </li>
                            <li>
                                <a class="pic" href="#"><img _src="images/wyPic2.jpg" src="images/blank.png"/></a>
                                <!--<p>日皇太子妃10年首出访</p>-->
                            </li>
                            <li>
                                <a class="pic" href="#"><img _src="images/wyPic3.jpg" src="images/blank.png"/></a>
                                <p>武僧一龙一拳击败日本</p>
                            </li>
                            <li>
                                <a class="pic" href="#"><img _src="images/wyPic4.jpg" src="images/blank.png"/></a>
                                <p>被免职的雅安乡官</p>
                            </li>
                            <li>
                                <a class="pic" href="#"><img _src="images/wyPic5.jpg" src="images/blank.png"/></a>
                                <p>火箭2分险胜雷霆总分1-3</p>
                            </li>
                            <li>
                                <a class="pic" href="#"><img _src="images/wyPic6.jpg" src="images/blank.png"/></a>
                                <p>杨幂着透视长裙亮相</p>
                            </li>
                        </ul>
                    </div>

js如下

     var $ul = $('#section5 .scrollBox1 .bd ul');
        $ul.each(function () {
            var $nul = $('<ul></ul>');
            var $curUl = $(this);
            $curUl.children().each(function (idx) {
                console.log(idx);
                $(this).appendTo($nul);
                if ((idx + 1) % 4 === 0) {
                    $nul.appendTo($curUl.parent());
                    $nul = $('<ul></ul>');
                }
            });
            if ($nul.children().length != 4) {
                $nul.appendTo($curUl.parent());
            }
            $curUl.remove();
        });

这里面,我不太明白写idx是什么意思?我用console看过,结果是这样

clipboard.png
所以这个idx到底是什么??另外这句if ((idx + 1) % 4 === 0)又该怎么理解呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

http://www.css88.com/jqapi-1....

function(index, Element)
类型: Function()

为每个匹配元素执行的一个函数。

比如你的ul有六个li,index为序号,从0开始

if ((idx + 1) % 4 === 0)就是第四个(或者四的倍数个)就执行条件的内容。(+1变成 从1开始算,能被4整除,不就是4,8,12那些了么)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...