JQMIGRATE:jQuery.fn.offset() 需要一个连接到文档的元素 [英] JQMIGRATE: jQuery.fn.offset() requires an element connected to a document

查看:16
本文介绍了JQMIGRATE:jQuery.fn.offset() 需要一个连接到文档的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 jquery 1.x 的站点....现在,我已经开始在该站点中使用 jquery 3.x..... 为了解决任何迁移问题,我已经安装了 JQMigrate.

I have a site which used jquery 1.x.... Now, I have started using jquery 3.x in this site..... to solve any migration issues, I have installed JQMigrate.

它在控制台窗口中显示的消息之一是JQMIGRATE:jQuery.fn.offset() 需要一个连接到文档的元素".

One of the messages it shows in console window is "JQMIGRATE: jQuery.fn.offset() requires an element connected to a document".

我不确定如何解决它.唯一被称为解决方案"的是:不要试图获取或设置无效输入的偏移信息."

I am not sure how I can solve it. The only thing that is said as a "solution" is: "Do not attempt to get or set the offset information of invalid input."

这很明显,但在实践中意味着什么?如果我有,例如,

That is obvious, but what does it mean in practice? if I have, for example,

var parentOffset = $offsetParent.offset();

我应该像这样写那行吗?

Should I write that line something like this?

var parentOffset = $offsetParent ? $offsetParent.offset() : 0;

是否真的有必要,因为我知道 $offsetParent 总是一个有效的输入.

Is it really necessary since I know that $offsetParent is always a valid input.

问候詹姆

推荐答案

我刚遇到同一行代码.

这里的问题是 $offsetParent 元素不是 document 的一部分.

The problem here is that the $offsetParent element is not part of the document.

与其检查 $offsetParent.length,我们希望确保该元素实际上是 document

Instead of checking $offsetParent.length we want to make sure this element is actually part of the document

// As of jQuery 3.0, .offset() only works for elements that are currently
// in the document. In earlier versions, this would return the value below
// but in jQuery 3.0 this throws an error.
var parentOffset = {top: 0, left: 0};

// If the element is in the document we are safe to use .offset()
if(document.body.contains($offsetParent[0])) {
    parentOffset = $offsetParent.offset();
}

参见 https://github.com/jquery/jquery-migrate/blob/master/warnings.md#jqmigrate-jqueryfnoffset-requires-an-element-connected-to-a-document有关此更改的更多信息.

See https://github.com/jquery/jquery-migrate/blob/master/warnings.md#jqmigrate-jqueryfnoffset-requires-an-element-connected-to-a-document for more information on this change.

这篇关于JQMIGRATE:jQuery.fn.offset() 需要一个连接到文档的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆