我有这段代码可以一页:-

$(window).load(function() {
  function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'');
  }

  $('a[href*=#]').each(function() {
    if ( filterPath(location.pathname) == filterPath(this.pathname)
        && location.hostname == this.hostname
        && this.hash.replace(/#/,'') ) {
            var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
            var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : true;
            if ($target) {
                var targetOffset = $target.offset().top + 50;
                $(this).click(function(e) {
                    e.preventDefault();
                    $('html, body').animate({scrollTop: targetOffset}, 1400);
                    var d = document.createElement("div");
                    d.style.height = "90%";
                    d.style.overflow = "hidden";
                    document.body.appendChild(d);
                    window.scrollTo(0,scrollToM);
                    setTimeout(function() {
                        d.parentNode.removeChild(d);
                    }, 10);
                    return false;
                 });
             }
         }
     });
});


有告诉我这个错误:-

未捕获的ReferenceError:未定义scrollToM

解决办法是什么

最佳答案

您使用了window.scrollTo(0,scrollToM);,但我什至看不到您在哪里定义了变量。检查是否定义了变量scrollTom。

关于javascript - 为什么会出现Uncaught ReferenceError:未定义scrollToM?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25852995/

10-11 23:26