HTML: Соглашения о присвоении имен для идентификационных атрибутов

В последнее время я делал большую работу фронтенда. Некоторые разработчики здесь называли свои вещи элементов как "divPhotoGalleryContainer", и иногда я буду просто видеть "galleryContainer".

Существует ли хорошее соглашение о присвоении имен? Добавление "отделения" действительно полезно?

6
задан JamesBrownIsDead 16 December 2009 в 01:38
поделиться

5 ответов

The stupid thing is, Hungarian notation like divPhotoGalleryContainer is totally unnecessary with CSS. You can name the ID PhotoGalleryContainer and target it to a

element in the CSS:

div#PhotoGalleryContainer {
  /* rules */
}

Inside that rule you can usually assume certain properties like display: block, unless you're targeting generic divs somewhere else (kinda bad practice).

There aren't really any specific conventions for naming, just use names that are clear and simple.

3
ответ дан 17 December 2019 в 00:10
поделиться

I don't think it's particularly useful, but I don't think it's harmful either. Consistency is the most important convention.

2
ответ дан 17 December 2019 в 00:10
поделиться

The best naming convention is the one that makes sense to the developers/designers involved in the project. Given the two examples in your question, I'd be willing to bet that the "divPhotoGalleryContainer" contains "div" because either: it's referenced in a CSS selector, or some javascript code is looking at it and it's somehow helpful to know what type of element the id is referring to.

The "divPhotoGalleryContainer" convention seems like an HTML-ish flavor of Hungarian notation.

1
ответ дан 17 December 2019 в 00:10
поделиться

The most important thing is that you're consistant with whatever method you use.

However, I've always found it helpful to use the hungarian type notation of "divPhotoGalleryContainer", and "txtLastName". It makes it easier to distinguish page elements from other variables, both client and server side.

0
ответ дан 17 December 2019 в 00:10
поделиться

В этом случае не вредно добавлять "div". Как сказал DisgruntledGoat, венгерская нотация может быть бесполезной с CSS (то есть, если вы не хотите ограничивать класс одним типом элемента), и комментарий Роба правильный, вы даже можете изменить свои элементы и сохранить те же классы / идентификаторы , но в последнее время может быть полезно лучше понять код.

Я всегда использую венгерскую нотацию, потому что привык к ней. Если вы к чему-то привыкли, оставьте это, так как это проще, чем менять. В средах, где многие кодеры пишут одни и те же вещи, вы можете писать как хотите, если нет соглашения. Однако быть слишком описательным не так плохо, как быть недостаточно описательным. Тем не менее, я голосую за исчерпывающие имена для всего, включая переменные, функции, классы, идентификаторы, элементы XML и т. Д. Если становится трудно читать, используйте больше / лучше размещенных пробелов / строк.

0
ответ дан 17 December 2019 в 00:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: