jquery滚动上的目标为null


Target null on jquery scroll

我一直在尝试实现一个滚动脚本,该脚本将从菜单项名称平滑过渡到相关的a name值。这是脚本:

$('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
    }, 1000);
    return false;
    }
    }
    }); 

这是在Joomla 3网站上实施的。我在CMS之外的一个空白php脚本上测试了这个脚本,它似乎工作得很好。控制台错误读取为TypeError: target is null

target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

正如你所说的,它在seprate页面上工作,所以,你检查一下可能是你的jquery冲突了!!!把你的$符号换成joomla。

希望能帮到你!!