How to access object properties containing special

2018-12-31 00:26发布

I have a form DOM element:

var virDom = document.getElementsByTagName("form")[0];

virDom has two fields with IDs creditId and pwdId... I can access virDom.creditId without any issue but, virDom.pwdId.. is failing with a syntax error, because of the periods contained in the name.

How can I access such properties?

标签: javascript
1条回答
刘海飞了
2楼-- · 2018-12-31 01:32

Use bracket notation:

virDom['creditId']
virDom['pwdId..']

This applies to any object, and it is particularly useful for non-identifier-safe characters and also for accessing keys that you may not know ahead of time.

查看更多
登录 后发表回答