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

前端 未结 6 1897
感动是毒
感动是毒 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:40

    when is marginLeft being used:

    $("div").css({
        marginLeft:'12px',
        backgroundPosition:'10px -10px',
        minHeight: '40px'
    });
    

    As you can see, attributes that has a hyphen on it are converted to camelcased format. Using the margin-left from the previous code block above would make JavaScript bonkers because it will treat the hyphen as an operation for subtraction.

    when is margin-left used:

    $("div").css("margin-left","12px").css("background-position","10px -10px").css("min-height","40px");
    

    Theoretically, both code blocks will do the same thing. We can allow hyphens on the second block because it is a string value while compared to the first block, it is somewhat an object.
    Now that should make sense.

提交回复
热议问题