Custom attributes - Yea or nay?

前端 未结 14 1741
南旧
南旧 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条回答
  •  -上瘾入骨i
    2020-11-22 03:26

    I'm not doing using custom attributes, because I'm outputing XHTML, because I want the data to be machine-readable by 3rd-party software (although, I could extend the XHTML schema if I wanted to).

    As an alternative to custom attributes, mostly I'm finding the id and class attributes (e.g. as mentioned in other answers) sufficient.

    Also, consider this:

    • If the extra data is to be human-readable as well as machine-readable, then it needs to be encoded using (visible) HTML tags and text instead of as custom attributes.

    • If it doesn't need to be human readable, then perhaps it can be encoded using invisible HTML tags and text.

    Some people make an exception: they allow custom attributes, added to the DOM by Javascript on the client side at run-time. They reckon this is OK: because the custom attributes are only added to the DOM at run-time, the HTML contains no custom attributes.

提交回复
热议问题