我在看http://docs.jquery.com/Plugins/Authoring#Defaults_and_Options为jQuery创建一个简单的插件。在关于选项和设置的部分之后,我做了以下操作,但无效(脚本在遇到设置时退出)。是否允许在JavaScript属性名称中使用破折号?
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文档错了吗?
请注意,您正在尝试使用'background-color'作为属性存取器,而不是作为变量。变量只能是标识符,属性访问器不太严格,可以是标识符名称(不包括保留字)。但在这种情况下,'background-color'既不是标识符也不是标识名。 – Oriol 2016-03-15 21:35:23