获取所有自定义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); });更多推荐
发布评论