Node.setUserData() - Web API 接口参考 编辑

已废弃
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

The Node.setUserData() method allows a user to attach (or remove) data to an element, without needing to modify the DOM. Note that such data will not be preserved when imported via Node.importNode, as with Node.cloneNode() and Node.renameNode() operations (though Node.adoptNode does preserve the information), and equality tests in Node.isEqualNode() do not consider user data in making the assessment.

This method offers the convenience of associating data with specific nodes without needing to alter the structure of a document and in a standard fashion, but it also means that extra steps may need to be taken if one wishes to serialize the information or include the information upon clone, import, or rename operations.

The Node.getUserData and Node.setUserData methods are no longer available from Web content. Element.dataset or WeakMap can be used instead.

Syntax

prevUserData = someNode.setUserData(userKey, userData, handler);

Parameters

  • userKey is used as the key by which one may subsequently obtain the stored data. More than one key can be set for a given node.
  • handler is a callback which will be called any time the node is being cloned, imported, renamed, as well as if deleted or adopted; a function can be used or an object implementing the handle method (part of the UserDataHandler interface). The handler will be passed five arguments: an operation type integer (e.g., 1 to indicate a clone operation), the user key, the data on the node, the source node (null if being deleted), the destination node (the newly created node or null if none).If no handler is desired, one must specify null.
  • userData is the object to associate to userKey on someNode. If null, any previously registered object and UserDataHandler associated to userKey on this node will be removed.

Example

var d = document.implementation.createDocument('', 'test', null);
d.documentElement.setUserData('key', 15, {handle:function (o, k, d, s, ds) {alert(o+'::'+k+'::'+d+'::'+s+'::'+ds)}}); // 2::key::15::[object Element]::[object Element]
alert(d.documentElement.getUserData('key')); // 15
var e = document.importNode(d.documentElement, true); // causes handler to be called
alert(e.getUserData('key')); // null since user data is not copied

Specifications

SpecificationStatusComment
DOM
Node
Living StandardRemoved from the specification.
Document Object Model (DOM) Level 3 Core Specification
Node.setUserData()
ObsoleteInitial definition.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support未实现Supported from 1.0 (1.7 or earlier) to 21.0 (21.0).
Removed in 22.0 (22.0) [1]
?未实现未实现
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support未实现Supported from 1.0 (1.0) to 21.0 (21.0).
Removed in 22.0 (22.0) [1]
(Yes)未实现未实现

[1] The method is still available from chrome scripts.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:85 次

字数:7814

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文