变量未传递到 jQuery 滚动函数

Variable not passed into jQuery scroll function

谁能帮我理解为什么这不起作用:

   jQuery(".page-code .opening .button").click(function(event){
        event.preventDefault();
        var id = "#" + event.target.id;
        alert(id);
        jQuery('html, body').animate({
            scrollTop: jQuery(id).offset().top
        }, 500);
    })

锚链接存在于页面的各个部分,警报为我提供了我需要的正确 ID,但无论我单击哪个按钮,页面都只会滚动一点点。不确定我在这里遗漏了什么。

如果您希望用户点击 .page-code OR .opening OR .button 元素,然后在它们之间添加逗号。

您正在将滚动的目标设置为刚刚单击的元素。如果你想滚动到 link 指向的元素,你需要使用 href 属性(假设你的按钮是 a 标签):

jQuery(".page-code .opening .button").click(function(event){
    event.preventDefault();
    var id = "#" + jQuery(this).attr('href');
    alert(id);
    jQuery('html, body').animate({
        scrollTop: jQuery('body').offset().top
    }, 500);
})