简体   繁体   English

如何获取 DOM 元素的所有属性和方法

[英]How do I get all the properties and methods of a DOM element

I can get all the properties and methods of a DOM element in the following way我可以通过以下方式获取一个DOM元素的所有属性和方法

var e = document.createElement('div')
let arr = []
for(let key in e){
  arr.push(key)
  console.log(key)
  //cloneNode
  //...
}

But I want to know why the following methods are not available?但是我想知道为什么以下方法不可用?

const arr = Object.key(e)
//result: []

const arr2 = Object.getOwnPropertyNames(e)
//result: []

why?为什么?

Assuming that:假如说:

How do I get all the properties and methods of a DOM element and store them in an array如何获取 DOM 元素的所有属性和方法并将它们存储在数组中

It is because these built-in properties of a DOM object are enumerable but are not own properties.这是因为 DOM object 的这些内置属性是可枚举的,但不是自己的属性。 In fact these properties are inherited properties.实际上这些属性是继承的属性。 If a property is enumerable but not own-property propertyIsEnumerable method will give false .如果一个属性是可枚举的但不是自己的属性propertyIsEnumerable方法将给出false

在此处输入图像描述

Observe the example below:观察下面的例子:

 let inp = document.getElementsByClassName('someclass')[0] console.log(inp.propertyIsEnumerable('onclick'))
 <input type='range' class='someclass' min='0' max='100' />

As the output of the above code snippet gives out false , that means the property onclick is enumerable but not an own property.由于上述代码片段的 output 给出了false ,这意味着属性onclick是可枚举的,但不是自己的属性。 Any enumerable properties appear in for...in loop.任何可枚举的属性都出现在for...in循环中。 But only enumerable and own properties appear in Object.keys method.但是Object.keys方法中只出现了可枚举和自己的属性。 That is why you get the value in for...in loop but not with Object.keys这就是为什么你在for...in循环中获得值但不是Object.keys

As VLAZ mentioned, many of those properties you want to retrieve come from the prototype chain.正如 VLAZ 所提到的,您要检索的许多属性都来自原型链。 In order to store all of them in an array, we will actually need to loop through the prototype chain using recursion.为了将它们全部存储在一个数组中,我们实际上需要使用递归遍历原型链。

Using recursion will allow us to create a simple function we can use to quickly retrieve all available properties and methods for any node or object without having to set up a for..in loop each time.使用递归将允许我们创建一个简单的 function,我们可以使用它来快速检索任何节点或 object 的所有可用属性和方法,而无需每次都设置for..in循环。 Here is how I would achieve this:以下是我将如何实现这一目标:

 var e = document.createElement('div'); const getAllProperties = (e, props = []) => e.__proto__? getAllProperties(e.__proto__, props.concat(Object.getOwnPropertyNames(e))): [...new Set(props.concat(Object.getOwnPropertyNames(e)))]; const divProps = getAllProperties(e); console.log(`ALL ${e.constructor.name} properties (${divProps.length})`, divProps);

You could even take this one step further and return each property and method available along with the values associated with keys by mapping them back to the original node/object in a new key-value object:您甚至可以更进一步,通过将它们映射回新键值 object 中的原始节点/对象,返回每个可用的属性和方法以及与键关联的值:

 const e = document.createElement('div'); const getAllProperties = (o, e = o, props = []) => e.__proto__? getAllProperties(o, e.__proto__, props.concat(Object.getOwnPropertyNames(e))): Object.fromEntries([...new Set(props.concat(Object.getOwnPropertyNames(e)))].map(prop => [prop, o[prop]])); const divProps = getAllProperties(e); console.log(`ALL ${e.constructor.name} properties (${Object.keys(divProps).length})`, divProps);

I am assuming you only need the keys and not their values.我假设您只需要键而不是它们的值。

First, confirm if the var e as you defined below is indeed an object.首先,确认您在下面定义的 var e 是否确实是 object。

var e = document.createElement('div')

Second, the correct method that returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would is below其次,返回给定对象自己的可枚举属性名称数组的正确方法,以与正常循环相同的顺序迭代如下

Object.keys(e) 

MDN link for reference MDN链接供参考

If you refer to the MDN docs you will find the below example如果您参考 MDN 文档,您将找到以下示例

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM