0

如何遍历所有添加的 DOM 属性(不是浏览器属性)?

示例 DOM:

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

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

4

1 回答 1

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  );
于 2013-04-07T19:58:19.317 回答