Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接到hashtag_Javascript_Jquery_Html - Fatal编程技术网

Javascript 链接到hashtag

Javascript 链接到hashtag,javascript,jquery,html,Javascript,Jquery,Html,我需要一些东西把这个链接起来 $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.has

我需要一些东西把这个链接起来

$(function() {
    $('a[href*=#]: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-55
                }, 1000);
                return false;
            }
        }
    });
});

但它只适用于该链接
。我希望此代码适用于此链接,例如

我认为您的jquery选择器是错误的


请注意,片段标识符有时很难测试:如果您在地址栏中手动更改它,您将需要强制页面刷新,因为只需按enter键实际上不会有多大作用。
 $(function() {
       $("a[href*='#']:not([href='#'])").click(function(event) {
            location = $(this).attr('href');
            event.stopPropgation();
            return true;
       });
    });