获取所有自定义DOM属性(Get all custom DOM properties)

编程入门 行业动态 更新时间:2024-10-22 18:25:39
获取所有自定义DOM属性(Get all custom DOM properties)

我正在动态地将DOM属性附加到元素:

i = 0; document.body['a' + i] = "foo"; document.body['b' + i] = "bar";

有没有办法可以获得我作为数组附加的所有属性? 例:

var allProperties = ['a0', 'b0'];

谢谢!

I am attaching DOM properties to an element dynamically like so:

i = 0; document.body['a' + i] = "foo"; document.body['b' + i] = "bar";

Is there a way I can get all of those properties that I attached as an array? Example:

var allProperties = ['a0', 'b0'];

Thanks!

最满意答案

您实际上可以创建一个新的主体对象,并与该对象进行比较以获取浏览器未添加的属性

i = 0; document.body['a' + i] = "foo"; document.body['b' + i] = "bar"; var el = document.createElement('body'), arr = []; for (var key in document.body) { if (document.body.hasOwnProperty(key) && !(key in el)) { arr.push(key); } }

小提琴

更加花哨

var el = document.createElement('body'), arr = Object.keys(document.body).filter(function(prop) { return !(prop in el); });

You can actually create a new body object, and compare against that object to get properties not added by the browser

i = 0; document.body['a' + i] = "foo"; document.body['b' + i] = "bar"; var el = document.createElement('body'), arr = []; for (var key in document.body) { if (document.body.hasOwnProperty(key) && !(key in el)) { arr.push(key); } }

FIDDLE

being a little more fancy

var el = document.createElement('body'), arr = Object.keys(document.body).filter(function(prop) { return !(prop in el); });

更多推荐

本文发布于:2023-04-29 03:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性   properties   DOM   custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!