body.scrollTop在严格模式下已弃用。 如果处于严格模式,请使用’documentElement.scrollTop’;如果处于怪癖模式,请使用’body.scrollTop’。
我收到错误:
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'); }); });
如何编辑我的代码以使用此documentElement.ScrollTop?
Dagg Nabbit给出了解决方案。 更改
$('html,body').animate({scrollTop: divTag.offset().top},'slow');
至
$('html').animate({scrollTop: divTag.offset().top},'slow');
如果您想避免Chrome中的弃用警告。 ( 为什么不推荐使用body.scrollTop
? )
它的工作原理是因为documentElement
是html
节点:
$('html')[0] === document.documentElement //-> true $('body')[0] === document.body //-> true
但是你的代码现在正在运行(尽管有警告),当Chrome删除“古怪”行为时它会继续工作。 如果要继续支持使用body.scrollTop
来代表标准模式下滚动视口的浏览器(我认为旧的Chrome和Safari),则不应更改代码。