我在看 http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options为 jQuery 创建一个简单的插件。在关于选项和设置的部分之后,我做了以下操作,但没有用(脚本在遇到设置时退出)。
var settings = {
'location' : 'top',
'background-color': 'blue'
}
...
$this.css('backgroundColor', settings.background-color); // fails here
一旦我从背景颜色中删除破折号,一切正常。
var settings = {
'location' : 'top',
'backgroundColor': 'blue' // dash removed here
}
...
$this.css('backgroundColor', settings.backgroundColor);
是我遗漏了什么,还是 jQuery 文档有误?
最佳答案
没有。解析器会将其解释为减法运算符。
你可以做 settings['background-color']
。
关于javascript - JavaScript 属性名称中是否允许使用破折号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5516106/