Replacement for DOM3 getUserData/setUserData methods

旧城冷巷雨未停 提交于 2020-01-04 11:43:33

问题


So we've got some old-school Firefox extension code that uses the DOM3 methods get/setUserData() to pass around data in the DOM. Unfortunately, these have been deprecated in DOM4, so Firefox is planning to drop support for them, and Chrome never supported them in the first place.

Is there a cross-browser replacement? jQuery's $.data seems to be an option, but 'pure' JavaScript would be preferable.


回答1:


So in the end we decided to go with jQuery after all, the core library is only around 75k and it solves the problem very cleanly:

element.getUserData('foo') --> $(element).data('foo')

element.setUserData('foo', 'bar', null) --> $(element).data('foo', 'bar')



回答2:


Use Custom event which can store data in the detail

Ref: How can I transfer data to firefox extension from web page




回答3:


Just set any property you want on the element.

element.key = value;

DOM elements/nodes are just JavaScript objects. To avoid name clashes with native properties (like id, etc.), you could prefix your keys with an underscore.

element._id = "foo";


来源:https://stackoverflow.com/questions/10943519/replacement-for-dom3-getuserdata-setuserdata-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!