body.scrollTop在严格模式下已弃用.如果处于严格模式,请使用'documentElement.scrollTop';如果处于怪癖模式,请使用'body.scrollTop'.

are*_*owe 6 javascript jquery deprecated

我收到错误:

body.scrollTop在严格模式下已弃用.如果处于严格模式,请使用'documentElement.scrollTop';如果处于怪癖模式,请使用'body.scrollTop'.

我的代码是:

$(document).ready(function(){

    //Animates Scrolling to anchor
    function scrollToAnchor(aid){
        var divTag = $("div[name='"+ aid +"']");
        $('html,body').animate({scrollTop: divTag.offset().top},'slow');
    }

    //If Checking out as guest, scroll to Shipping Information
    $("#ReadDescription").click(function() {
        scrollToAnchor('longdescreadmore');
    });

});
Run Code Online (Sandbox Code Playgroud)

如何编辑我的代码以使用此documentElement.ScrollTop?

sam*_*sam 15

Dagg Nabbit给出了解决方案.更改

$('html,body').animate({scrollTop: divTag.offset().top},'slow');
Run Code Online (Sandbox Code Playgroud)

$('html').animate({scrollTop: divTag.offset().top},'slow');
Run Code Online (Sandbox Code Playgroud)

如果您想避免Chrome中的弃用警告.(为什么body.scrollTop弃用?)

它的工作原理,因为documentElementhtml节点:

$('html')[0] === document.documentElement //-> true
$('body')[0] === document.body            //-> true
Run Code Online (Sandbox Code Playgroud)

但是你的代码现在正在运行(虽然有警告),当Chrome删除"古怪"行为时它会继续工作.你不应该,如果你想继续支持使用浏览器更改您的代码body.scrollTop来表示的标准模式(旧的Chrome和Safari浏览器,我认为)滚动视口.