【问题标题】:JQuery .each() callback function doesn't run on each iteration/loopJQuery .each() 回调函数不会在每次迭代/循环上运行
【发布时间】:2010-11-21 06:42:34
【问题描述】:

这是应该发生的事情。
1.获取点击链接的rel属性
2. 对于每个具有“条目”类的 div:
(i)获取它的“左”位置
(ii) 计算其外部高度
(iii) 遍历“a.tag_filter”的所有实例。如果它在“rel”中找到与最初单击的字符串相同的字符串,则将 1 添加到“V”并跳出循环。
(iv)如果循环后“V”等于 0,我们知道该“.entry”中不存在相同的标签,因此将其淡出。
(v)一旦淡出完成循环遍历淡出后的所有“.entry”并获取它们的“左”值。
(vi) 如果褪色条目的左值 = 当前 '.entry' 的左值,则将其重新定位到新的 'top' 值。

目前正在发生什么。
它遍历并淡出所有正确的“.entry”元素,只有在所有元素都淡出后,它才会重新定位剩余的“.entry”元素。

在每个元素淡出后,我希望重新定位循环运行,这样它基本上一次定位剩余元素,而不是一次定位所有元素。

这是我的代码编辑:

$('a.tag_filter').click(function(e){
        e.preventDefault();
        var selectTag = $(this).attr('rel');

    $('div.spotlight_entry_container_grid').each(function(i){
        var $entry = $(this);
        var tagArray = [];

        $('a.tag_filter', this).each(function(){
            tagArray.push ($(this).attr('rel'));
        }); 

        if($.inArray(selectTag,tagArray) == -1){
            var leftPos = $entry.css("left"); 
                    var topPos = $entry.css("top"); 

            $entry.fadeOut(1000, function(){
                var nextLeftPos;
                            var nextTopPos;

                $('div.spotlight_entry_container_grid:gt('+i+')').each(function(j) {   
                    var $laterEntry = $(this); 
                    nextLeftPos = $laterEntry.css("left");
                                nextTopPos = $laterEntry.css("top");
                    //we need to keep the entries in their columns.
                    //matching left values will do it. No need to animate left values.
                    if(leftPos == nextLeftPos){
                        $laterEntry.animate({ top: topPos});
                    }
                    }); 
            });
        }   
    });
    });

希望这是有道理的
任何帮助,将不胜感激! 我可能正在做一些疯狂的事情,但我无法发现它。 谢谢

【问题讨论】:

    标签: javascript jquery loops iteration each


    【解决方案1】:

    在这里

    $('a.tag_filter', this).each(function(){
                            var curTag = $(this).attr('rel');
                            if(curTag == selectTag){
                                    v++;
                                    return false;
                            }
                    }); 
    

    $().each() 内部返回 false 会中断对包装集中每个元素的循环。

    来自documentation

    从 each 中返回 'false' 函数完全停止循环 通过所有元素(这是 就像在正常情况下使用“休息” 环形)。从内部返回“真” 循环跳到下一次迭代 (这就像使用“继续” 一个正常的循环)。

    另外,为了提高性能,我建议在每个 each() 中缓存 $(this),而不是多次引用它。

    编辑:

    进一步查看代码后,我认为以下应该这样做

    $('a.tag_filter').click(function(e){
    
            // prevent the default anchor behaviour
            e.preventDefault();
            var selectTag = $(this).attr('rel');
    
            $('div.entry').each(function(i){
                    var $entry = $(this);                  
    
                    // get an array of the anchor tag rel attributes
                    var tagArray = [];
                    $('a.tag_filter', this).each(function() {
                            tagArray.push ($(this).attr('rel'));
                    });
    
                    // if we can't find the selected tag in the entries tags
                    if ($.inArray(selectTag,tagArray) == -1) {        
                        var leftPos = $entry.css("left"); 
                        var topPos = $entry.css("top");                      
    
                        $entry.fadeOut(1000, function(){
                            var nextLeftPos;
                            var nextTopPos;
    
                            $('div.entry:gt('+i+')').each(function(j) {             
                                var $laterEntry = $(this); 
                                nextLeftPos = $laterEntry.css("left");
                                nextTopPos = $laterEntry.css("top");
                                // for the first element, set top and left to the faded out element values
                                if (j == 0) {                               
                                    $laterEntry.animate({ top: topPos, left: leftPos });
                                }
                                // for later elements in the loop, ste the values to the previous element values
                                else {
                                    $laterEntry.animate({ top: nextTopPos, left: nextLeftPos  });
                                }
                            });
                        });                                                                  
                    }
             });  
    
    
    
     });
    

    【讨论】:

    • 之所以存在,是因为一旦 'rel' 属性与存储在 selectTag 中的字符串匹配,则无需继续循环。如果我删除 return false 前端没有任何变化
    • 我不确定这会如何工作。当然,这会在 a.tag_filter 的每个实例上运行 if(v==0) 块.....redeemercentral.com/index.php/test/grid 开发页面,这样你就可以看到发生了什么。单击白色宝丽来样式框中的标签。
    • 如果你点击“生活”,你会看到所有没有“生活”标签的条目都会淡出,然后剩下的会向上滑动。但是,中间列不会滑到顶部,这向我表明,每次条目淡出时都不会调用“重新定位” .each() 函数,因此它只会向上移动一次。
    • 正在被调用,否则元素根本不会被重新定位。我们这里的情况一定是在顶部位置计算上出现了某种错误。
    • Russ 使用上面的代码,我们不能将块放入 if(curTag != selectTag) 中,因为很多时候都是这样。如果 SelectTag 中的字符串不等于 a.tag_filter rel 属性中的任何一个,我们只需要删除该条目。
    【解决方案2】:

    你不需要缓存 $(this),jQuery 自动缓存 this 选择器用于函数回调。

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      相关资源
      最近更新 更多