简体   繁体   中英

Function within a function jquery

I need to run 2 functions upon one click event and can't figure out how to nest them.

Currently both work if run separately but not together.

This function closes my menu after click.

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

And this function scrolls to my specific anchors.

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;
    }
  }
});

I can't figure out how and where to place the top function within the click event below. This is possible right?

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

从两个函数中删除return false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM