2014-10-17 72 views
1

我需要在一次單擊事件時運行2個函數,但無法弄清楚如何嵌套它們。函數jQuery中的函數

目前,這兩個工作如果單獨運行,但不能在一起>我只是潛入Jquery,如此裸露與我。

此功能在點擊後關閉我的菜單。

$(document).on('click','.navbar-collapse.in',function(e) { 
    if($(e.target).is('a')) { 
    $(this).collapse('toggle');  
    } 
}); 

而這個函數會滾動到我的指定錨點。

function scrollToAnchor() { 
    if($(".jquery-anchor").length > 0 && document.URL.indexOf("#") >= 0){ 
    var anchor = document.URL.split("#")[1]; 
    $(".jquery-anchor").each(function() { 
     if($(this).attr("name") == anchor) { 
     $("html,body").animate({ 
      scrollTop: $(this).offset().top - 50}, 
      'slow'); 
     } 
    });  
    } 
} 


$(function() { 
    $("a[href*='#JDG']:not([href='#'])").click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
     && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
     $('html,body').animate({ 
      scrollTop: target.offset().top - 30 //offsets for fixed header 
     }, 1000); 
     return false; 
     } 
    } 
    }); 

    //Executed on page load with URL containing an anchor tag. 
    if($(location.href.split("#")[1])) { 
    var target = $('#'+location.href.split("#")[1]); 
    if (target.length) { 
     $('html,body').animate({ 
     scrollTop: target.offset().top - 30 //offset height of header here too. 
     }, 1000); 
     return false; 
    } 
    } 
}); 

我不明白如何以及在哪裏放置頂部函數在下面的點擊事件。這是可能的嗎?

$("a[href*='#JDG']:not([href='#'])").click(function() { 
}); 

回答

5

刪除return false從兩者的功能

+0

+1 OMG它是如此簡單.......謝謝碧玉 – Torads 2014-10-17 13:41:42

+0

+1你想爲什麼別人的好處解釋@JasperSeinhorst? – PeterKA 2014-10-17 13:48:14

+0

返回false將阻止其他處理程序執行 – 2014-10-17 13:49:46