函数jquery中的函数

Function within a function jquery

本文关键字:函数 jquery      更新时间:2023-09-26

我需要在单击事件时运行两个函数,但不知道如何嵌套它们。

目前,如果单独运行,但不能一起运行,则两者都可以工作。

单击后,此功能将关闭我的菜单。

$(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() { 
});

从两个函数中删除return false