Javascript and CSS, using dashes

前端 未结 7 1983
小蘑菇
小蘑菇 2020-12-05 15:15

I\'m starting to learn some javascript and understand that dashes are not permitted when naming identifiers. However, in CSS it\'s common to use a dash for IDs and classes

7条回答
  •  暖寄归人
    2020-12-05 15:27

    Whenever you have to address a CSS property as a JavaScript variable name, CamelCase is the official way to go.

    element.style.backgroundColor = "#FFFFFF";
    

    You will never be in the situation to have to address a element's ID as a variable name. It will always be in a string, so

    document.getElementById("my-id");
    

    will always work.

提交回复
热议问题