Javascript and CSS, using dashes

前端 未结 7 1986
小蘑菇
小蘑菇 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:31

    IDs are allowed to contain hyphens:

    • ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

    And there is no restriction when using IDs in JavaScript except if you want to refer to elements in the global scope. There you need to use:

    window['css-dash-name']
    

提交回复
热议问题