jQuery for循环每三分之一

时间:2015-09-28 08:09:32

标签: javascript jquery

为什么我将i = 0更改为i = 1时却只有三分之一,但我却无法获得所有结果。

$.getJSON('search.php', {q: query, ajax: 'true'}, function(j){
    var options = '';  
    for (var i = 0; i < j.length; i++) {
        if(i % 3 == 0) {
            // every third
        } else {

        }
        $("#profile-search-results").html(options);
    }
});

2 个答案:

答案 0 :(得分:1)

我认为我们要做的是在索引3, 6, 9...等处获取元素,但是你的条件是针对第一,第五,第八......等元素执行的。

问题是你的循环以0开头,所以0%3将返回0 ...因为jQuery中的元素索引以0开头,你需要的是索引中的元素2, 5, 8,...等。因此,您应该检查reminder 3 == 2

for (var i = 0; i < j.length; i++) {
    if (i % 3 == 2) {

        // every third

    } else {



    }

答案 1 :(得分:0)

sourceRectmodulo运营商。

avc.modalPresentationStyle                   = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = self.view;
avc.popoverPresentationController.sourceRect = theButton.frame;
[self presentViewController:avc animated:YES completion:nil];

所以你拥有的是正常的。