访问所有自定义DOM属性

时间:2013-04-07 18:54:47

标签: dom

如何循环遍历所有添加的DOM属性(不是browser ones)?

示例DOM:

var div = document.createElement('div');
div['newProp1'] = '123';
div['newProp2'] = true;
div['someOtherProp'] = false;
...
...

假设我不知道“新”属性的名称,我该如何访问它们?

1 个答案:

答案 0 :(得分:0)

var div = document.createElement('div');
div['newProp1'] = '123';
div.newProp2 = true;
div['someOtherProp'] = false;

for( let prop in div )
    if( div.hasOwnProperty(prop) )
         console.log( prop  );