Using “data-*” attributes to replace the “id” attributes in HTML tags for CSS-Selecting Purpose

你离开我真会死。 提交于 2019-12-13 10:22:08

问题


Is it a good idea to use "data-" attributes to replace the "id" attributes in elements(tags) for css-selection purposes and then use those "data-*" attributes in automation testing scripts?


回答1:


No.

CSS selection and DOM-tree selection on id is much faster than selection any other attribute. Data-attribute select might be less efficient than selection on other attributes in somebrowsers.

The exception might be if you are abusing id attributes: storing something that is not just an identifier in them (e.g. composite data or something not unique). Selecting parts of id values, if supported, gives terrible performance. I would recommend using data attributes over abusing id attributes.




回答2:


Factually, you can use any of the attributes of an element.

So, Yes, you can use data-* attribute instead of id attribute as well provided the element is uniquely identified.


HTML data-* Attributes

  • The data-* attributes are used to store custom data private to the page or application.
  • The data-* attributes gives us the ability to embed custom data attributes on all HTML elements.
  • The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).
  • The data-* attributes consist of two parts:
    • The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix data-
    • The attribute value can be any string
  • Note: Custom attributes prefixed with data- will be completely ignored by the user agent.


来源:https://stackoverflow.com/questions/54053324/using-data-attributes-to-replace-the-id-attributes-in-html-tags-for-css-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!