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

前端 未结 6 1909
感动是毒
感动是毒 2020-12-02 22:22

With jQuery.css() I\'ve been told I can use the following two functions for the same results:

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

$(\".element\         


        
6条回答
  •  执念已碎
    2020-12-02 22:52

    jQuery's underlying code passes these strings to the DOM, which allows you to specify the CSS property name or the DOM property name in a very similar way:

    element.style.marginLeft = "10px";
    

    is equivalent to:

    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's not really doing anything special. It may alter or proxy some strings that you pass to .css(), but in reality there was no work put in from the jQuery team to allow either string to be passed. There's no extra resources used because the DOM does the work.

提交回复
热议问题