Custom attributes - Yea or nay?

前端 未结 14 1722
南旧
南旧 2020-11-22 03:02

Recently I have been reading more and more about people using custom attributes in their HTML tags, mainly for the purpose of embedding some extra bits of data for use in ja

14条回答
  •  孤独总比滥情好
    2020-11-22 03:22

    The easiest way to avoid use of custom attributes is to use existing attributes.

    use meaningful, relevant class names.
    For example, do something like: type='book' and type='cd', to represent books and cds. Classes are much better for representing what something IS.

    e.g. class='book'

    I have used custom attributes in the past, but honestly, there really isn't a need to for them if you make use of existing attributes in a semantically meaningful way.

    To give a more concrete example, let's say you have a site giving links to different kinds of stores. You could use the following:

    Molly's books
    James' Music
    

    css styling could use classes like:

    .store { }
    .cd.store { }
    .book.store { }
    

    In the above example we see that both are links to stores (as opposed to the other unrelated links on the site) and one is a cd store, and the other is a book store.

提交回复
热议问题