在严格模式下不推荐使用body.scrollTop.请使用' documentElement.scrollTop'如果在严格模式下' body.scrollTop'仅在怪癖模式下. [英] body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.
问题描述
我收到错误消息:
body.scrollTop在严格模式下已弃用.如果在严格模式下,请使用"documentElement.scrollTop";仅在怪癖"模式下,请使用"body.scrollTop".
body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode.
我的代码是:
$(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?
How can I edit my code to use this documentElement.ScrollTop?
推荐答案
Dagg Nabbit提出了解决方案.更改
Dagg Nabbit gave the solution. Change
$('html,body').animate({scrollTop: divTag.offset().top},'slow');
到
$('html').animate({scrollTop: divTag.offset().top},'slow');
如果要避免Chrome中的弃用警告.(为什么不推荐使用 body.scrollTop
?)
if you want to avoid the deprecation warning in Chrome. (Why is body.scrollTop
deprecated?)
之所以有效,是因为 documentElement
是 html
节点:
It works because documentElement
is the html
node:
$('html')[0] === document.documentElement //-> true
$('body')[0] === document.body //-> true
但是您的代码现在可以正常工作(尽管有警告),并且当Chrome删除古怪"行为后,它将继续正常工作.如果要继续支持使用 body.scrollTop
表示标准模式下的滚动视口的浏览器,则不应更改代码(我认为旧的Chrome和Safari)
But your code is working now (albeit with a warning) and it will keep working when Chrome removes the "quirky" behavior. You shouldn't change your code if you want to continue supporting browsers that use body.scrollTop
to represent the scrolling viewport in standards mode (older Chrome and Safari, I think).
这篇关于在严格模式下不推荐使用body.scrollTop.请使用' documentElement.scrollTop'如果在严格模式下' body.scrollTop'仅在怪癖模式下.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!