Best Practice for Naming the Id Attribute of Dom Elements

前端 未结 5 1470
半阙折子戏
半阙折子戏 2021-02-04 03:14

This is related to naming conventions for a DOM element\'s id attribute and I guess the name attribute as well. When it comes to JavaScript, from what I understand and have done

5条回答
  •  旧时难觅i
    2021-02-04 03:35

    Believe it or not, but I've had problems with hyphens as names of id's and css class names when using certain javascript libraries. This is very rare, but you obviously want to avoid something like this. Because of this I use camel case or underscores. You can use underscores as well.

    Otherwise the general rule is to have meaningful names that are easily read and understood. When it comes to "controls" make sure you follow some sort of a naming convention. Personally i prefer postfixes over prefixes (i.e. nameText instead of textName) but I try to avoid postfixes as I find them too verbose.

    So: 1. Meaningful names. 2. Avoid post-/prefix. 3. Avoid abbreviations (ie. address instead of addr). 4. Take your time.

提交回复
热议问题