Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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 #url问题中的NaN_Javascript_Jquery_Html - Fatal编程技术网

Javascript #url问题中的NaN

Javascript #url问题中的NaN,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试向我的网站添加平滑向下滚动功能,这样当用户从另一个页面单击锚定标记时,它会将用户链接/发送到另一个页面,并向下滚动到指定的div。但是,代码执行正确,但在url末尾会显示/#NaN 我的jQuery: jQuery(document).ready(function () { // run on DOM ready // grab target from URL hash (i.e. www.example.com/page-a.html#target-name)


我正在尝试向我的网站添加平滑向下滚动功能,这样当用户从另一个页面单击锚定标记时,它会将用户链接/发送到另一个页面,并向下滚动到指定的div。但是,代码执行正确,但在url末尾会显示/#NaN

我的jQuery:

jQuery(document).ready(function () {
    // run on DOM ready
    // grab target from URL hash (i.e. www.example.com/page-a.html#target-name)

    var target = window.location.hash;

    // only try to scroll to offset if target has been set in location hash

    if (target != '') {
        $('.introsection').hide(0);
        $('#swipe-down').hide(0);
        $('body').css('overflowY', 'scroll');
        $('#home-mobile-nav').css('position', 'fixed');
        $('#home').css('margin-top', '0');
        var $target = jQuery(target);
        jQuery('html, body').stop().animate({
                'scrollTop': $target.offset().top - 150
            }, // set offset value here i.e. 50
            2000,
            'swing',
            function () {
                window.location.hash = target - 40;
            });
    }
});
在控制台中会出现此错误,但我不知道如何修复此错误。

很可能是您的
$target
未定义,即未找到。尝试使用console.log($target)并查看是否获得元素首先需要检查页面中是否存在目标(
$target.length>0
)。最后,在您得到
#NaN
的地方,请注意您使用的是
target-40
,其中
target
应该是您的目标元素的ID(一个字符串!)test=
if(target!=''| target!==未定义)
,顺便说一句,如果在url的末尾出现了NaN,然后问题出在滚动页面之前的链接,因为它提供了一个未定义的hashtag