有人告诉我使用 jQuery.css() 可以使用以下两个函数来获得相同的结果:
$(".element").css("marginLeft") = "200px";
$(".element").css("margin-left") = "200px";
我一直使用 marginLeft
,因为这是文档中使用的内容:
Shorthand CSS properties (e.g. margin, background, border) are not supported. For example, if you want to retrieve the rendered margin, use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.
为什么 jQuery 允许 marginLeft
和 margin-left
?这似乎毫无意义并且使用更多资源来转换为 CSS margin-left
?
最佳答案
jQuery 的底层代码将这些字符串传递给 DOM,这允许您以非常相似的方式指定 CSS 属性名称或 DOM 属性名称:
element.style.marginLeft = "10px";
相当于:
element.style["margin-left"] = "10px";
Why has jQuery allowed for marginLeft as well as margin-left? It seems pointless and uses more resources to be converted to the CSS margin-left?
jQuery 并没有真正做任何特别的事情。它可能会更改或代理您传递给 .css()
的某些字符串,但实际上 jQuery 团队没有允许传递任一字符串.没有使用额外的资源,因为 DOM 完成了这项工作。
关于jQuery.css() - marginLeft 与 margin-left?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37606170/