为什么属性名称不同:
$(elem).css('padding-top')
和
$(elem).css('marginTop')
?不应该是:
$(elem).css('margin-top')
代替?
最佳答案
您可以使用“驼峰式”或“连字符分隔”形式的属性。两者都会起作用。查看jQuery docs for .css()
:
jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both
.css('background-color')
and.css('backgroundColor')
.
因此,要使用您的示例,以下所有方法都可行:
$(elem).css('marginTop')
$(elem).css('margin-top')
$(elem).css('paddingTop')
$(elem).css('padding-top')
关于jQuery CSS 属性名称不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771403/