gpt4 book ai didi

jQuery.css() - marginLeft 与 margin-left?

转载 作者:行者123 更新时间:2023-11-28 05:46:03 30 4
gpt4 key购买 nike

有人告诉我使用 jQuery.css() 可以使用以下两个函数来获得相同的结果:

$(".element").css("marginLeft") = "200px";

$(".element").css("margin-left") = "200px";

我一直使用 marginLeft,因为这是文档中使用的内容:

http://api.jquery.com/css/

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 允许 marginLeftmargin-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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com