Javascript and CSS, using dashes

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

    It's only in the cases where you can access the elements as properties that it makes a difference. For example form fields:

    In the onclick event you can't access the text box as a property, as the dash is interpreted as minus in Javascript:

    onclick="this.form.go-figure.value='Ouch!';"
    

    But you can still access it using a string:

    onclick="this.form['go-figure'].value='Ouch!';"
    

提交回复
热议问题