Javascript 循环执行if,如果为false则返回,如果为true=>;如果停止

Javascript 循环执行if,如果为false则返回,如果为true=>;如果停止,javascript,jquery,loops,if-statement,Javascript,Jquery,Loops,If Statement,因此,我得到了以下场景: $('.menu').find('a').each( function(el, val) { if( window.location.hash !== val.hash ) { $('.menu li').first().addClass('active') // we found no valid hash for us, so set first <li> active $('#hash1').show() // and show t

因此,我得到了以下场景:

$('.menu').find('a').each( function(el, val) {
  if( window.location.hash !== val.hash ) {
    $('.menu li').first().addClass('active') // we found no valid hash for us, so set first <li> active
    $('#hash1').show() // and show the first content
  } else {
    $(this).parent().addClass('active') // we found an hash, so give its parent <li> an active class
    $(window.location.hash).show() // can be #hash2, #hash3, whatever
    return false; // since we found something, stop the if.
  }
});
$('.menu')。查找('a')。每个(函数(el,val){
if(window.location.hash!==val.hash){
$('.menu li').first().addClass('active')//我们找不到有效的哈希值,所以将first
  • 设置为active $('#hash1').show()//并显示第一个内容 }否则{ $(this).parent().addClass('active')//我们找到了一个散列,所以给它的父类
  • 一个active类 $(window.location.hash).show()//可以是#hash2、#hash3等等 return false;//由于我们发现了某些内容,请停止if。 } });
  • 现在很明显,每次我们发现没有有效的散列,我们都会将第一个元素设置为活动的,并显示第一个内容。。。但我不想那样

    我希望if在进入else语句之前先遍历所有元素。。然后,如果没有找到任何内容,则将第一个元素设置为活动状态并显示第一个内容


    既然我在循环每个“a”,我该怎么做呢?

    只要在循环之外保留一个变量:

    var found = false;
    
    $('.menu').find('a').each( function(el, val) {
        if( window.location.hash === val.hash ) {
            $(this).parent().addClass('active'); // we found an hash, so give its parent <li> an active class
            $(window.location.hash).show(); // can be #hash2, #hash3, whatever
    
            found = true;
        }
    });
    
    
    if(!found) {
        $('.menu li').first().addClass('active'); // we found no valid hash for us, so set first <li> active
        $('#hash1').show(); // and show the first content
    }
    
    var-found=false;
    $('.menu')。查找('a')。每个(函数(el,val){
    if(window.location.hash==val.hash){
    $(this).parent().addClass('active');//我们找到了一个散列,所以给它的父类
  • 一个active类 $(window.location.hash).show();//可以是#hash2、#hash3,随便什么 发现=真; } }); 如果(!找到){ $('.menu li').first().addClass('active');//我们找不到有效的哈希值,所以将first
  • 设置为active $('#hash1').show();//并显示第一个内容 }

  • 此外,语句末尾的分号不是可选的。

    只需在循环外保留一个变量:

    var found = false;
    
    $('.menu').find('a').each( function(el, val) {
        if( window.location.hash === val.hash ) {
            $(this).parent().addClass('active'); // we found an hash, so give its parent <li> an active class
            $(window.location.hash).show(); // can be #hash2, #hash3, whatever
    
            found = true;
        }
    });
    
    
    if(!found) {
        $('.menu li').first().addClass('active'); // we found no valid hash for us, so set first <li> active
        $('#hash1').show(); // and show the first content
    }
    
    var-found=false;
    $('.menu')。查找('a')。每个(函数(el,val){
    if(window.location.hash==val.hash){
    $(this).parent().addClass('active');//我们找到了一个散列,所以给它的父类
  • 一个active类 $(window.location.hash).show();//可以是#hash2、#hash3,随便什么 发现=真; } }); 如果(!找到){ $('.menu li').first().addClass('active');//我们找不到有效的哈希值,所以将first
  • 设置为active $('#hash1').show();//并显示第一个内容 }
  • 此外,语句末尾的分号不是可选的。

    您可以使用
    .filter()
    获取所需的元素。如果未选择任何选项,则执行默认操作:

    var $links = $('.menu').find('a').filter(function() {
        return window.location.hash === this.hash;
    });
    
    if($links.length > 0) {
        $links.parent().addClass('active');
        $(window.location.hash).show();
    }
    else {
        $('.menu li').first().addClass('active');
        $('#hash1').show();
    }
    
    参考

    您可以使用
    .filter()
    获取所需的元素。如果未选择任何选项,则执行默认操作:

    var $links = $('.menu').find('a').filter(function() {
        return window.location.hash === this.hash;
    });
    
    if($links.length > 0) {
        $links.parent().addClass('active');
        $(window.location.hash).show();
    }
    else {
        $('.menu li').first().addClass('active');
        $('#hash1').show();
    }
    

    参考

    如果您能用英语更清楚地解释您的需求,我想您会发现JavaScript结构自然会遵循

    以下是我对您尝试执行的操作的最佳猜测:“.menu”中所有锚都具有与
    窗口.location.hash
    相同的
    .hash
    ,应将其父li设置为“活动”,并显示相应的元素。如果没有匹配项,则应将第一个菜单项设置为“活动”,并显示“#hash1”


    如果你能用英语更清楚地解释你的需求,我想你会发现JavaScript结构自然会遵循

    以下是我对您尝试执行的操作的最佳猜测:“.menu”中所有锚都具有与
    窗口.location.hash
    相同的
    .hash
    ,应将其父li设置为“活动”,并显示相应的元素。如果没有匹配项,则应将第一个菜单项设置为“活动”,并显示“#hash1”

    假设
    #hash1
    的标记与其余标记相同,并且浏览器地址栏中只有一个哈希(或者没有)


    假设
    #hash1
    的标记与其他标记相同,并且浏览器地址栏中只有一个哈希(或没有)?

    “此外,语句末尾的分号不是可选的。”是的。(一般来说,JavaScript中不是100%的情况,但在这段代码中肯定是100%的语句。)@nnnnnn:我重复一遍,不是可选的。是的,口译员会为您添加它们。是的,这是“合法的”。它也会产生糟糕的代码。非可选;)“JavaScript社区的思想领袖继续传播不确定性而不是理解,这是公然不负责任的。”似乎是一个未定义的
    “如果”中的这个
    比分号更重要。它仍然循环遍历元素并显示
    #hash1
    的内容,这正是OP不想要的。“此外,语句末尾的分号不是可选的。”是的,它们是可选的。(一般来说,JavaScript中不是100%的情况,但在这段代码中肯定是100%的语句。)@nnnnnn:我重复一遍,不是可选的。是的,口译员会为您添加它们。是的,这是“合法的”。它也会产生糟糕的代码。非可选;)“JavaScript社区的思想领袖继续传播不确定性而不是理解,这是公然不负责任的。”似乎是一个未定义的
    在你的'if'中,这个
    比分号更重要。它仍然在元素中循环并显示
    #hash1
    的内容,这正是OP不想要的。可能不是,我肯定
    过滤器在内部使用
    每个
    。我只是觉得它有点干净,因为它避免了一个额外的标志。他们之间几乎没有区别。谢谢可能不会,我确信
    过滤器
    正在内部使用
    每个
    。我只是觉得它有点干净,因为它避免了一个额外的标志。他们之间几乎没有区别。谢谢