Javascript and CSS, using dashes

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

    <form>
       <input type="text" name="go-figure" />
       <input type="button" value="Eat me!" onclick="...">
    </form>
    

    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!';"
    
    0 讨论(0)
提交回复
热议问题