Javascript and CSS, using dashes

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

    No, this won't cause an issue. You're accessing the ID as a string (it's enclosed in quotes), so the dash poses no problem. However, I would suggest not using document.getElementById("css-dash-name"), and instead using jQuery, so you can do:

    $("#css-dash-name");
    

    Which is much clearer. the jQuery documentation is also quite good. It's a web developers best friend.

提交回复
热议问题