过时
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.

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.

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

句法

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

参数

  • 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.

范例

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

规范

规范 状态 注释
DOM (文档对象模型) 3 级核心规范
The definition of 'Node.setUserData()' in that specification.
过时 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setUserData 弃用 非标 Chrome No Edge No Firefox 1 — 22 IE ? Opera No Safari No WebView Android No Chrome Android No Firefox Android 4 — 22 Opera Android No Safari iOS No Samsung Internet Android No

图例

不支持

不支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

另请参阅

元数据

  • 最后修改: