gpt4 book ai didi

javascript - 函数jquery中的函数

转载 作者:行者123 更新时间:2023-11-30 08:40:55 25 4
gpt4 key购买 nike

我需要在一次点击事件中运行 2 个函数,但不知道如何嵌套它们。

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

此功能会在点击后关闭我的菜单。

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

并且此功能滚动到我的特定 anchor 。

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

关于javascript - 函数jquery中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26426170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com