Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
element.style['property']='value'betweet betweet和element.style.property='value'javascript 中的操作样式有什么区别?
element.style['property']='value'
element.style.property='value'
没有区别,只是语法不同
在 JavaScript 中,对象也是关联数组(或哈希)。也就是说,财产
theStatus.Home
也可以通过调用来读取或写入
theStatus['Home']
对象作为关联数组
使用对象
在这种情况下有所不同,但我认为它是非标准的并且依赖于浏览器:在 Chrome 中,括号表示法允许您使用其实际 CSS 名称而不是驼峰式版本来引用样式属性。在 Firefox 中,它不起作用。我没有测试过其他浏览器。
您可以从控制台在此页面上使用 Chrome 进行尝试:
document.body.style['background-color'] = 'red'