简体   繁体   English

使用数据属性或类基于其状态设置元素的样式是否更好?

[英]Is it better practice to style an element based on its state with a data attribute or class?

I'm wondering what the best practice here is for styling an element based on its state. 我想知道这里最好的做法是根据状态设置元素。

I have some content that can take a while to load, whilst it's loading I want to show a loading spinner. 我有一些内容可能需要一段时间来加载,而它正在加载我想显示一个加载微调器。

Is it better to apply a class of 'loading' to the element until it's loaded, then remove this class with JS. 在加载之前对元素应用一个'loading'类更好,然后用JS删除这个类。 Using the 'loading' class to render the spinner. 使用'loading'类来渲染微调器。

Or is it better practice to disassociate the loading state from a class and use a data attribute, ie [data-status = "loading"]. 或者更好的做法是将加载状态与类解除关联并使用数据属性,即[data-status =“loading”]。 Then applying my styles to the data attribute rather than the class. 然后将我的样式应用于数据属性而不是类。

Cheers. 干杯。

Generally, class is the go-to for visual changes. 通常,课程是视觉变化的首选。

According to W3C... 根据W3C ......

data 数据

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements. 自定义数据属性旨在将自定义数据存储为页面或应用程序的私有数据,因为没有更合适的属性或元素。 These attributes are not intended for use by software that is independent of the site that uses the attributes. 这些属性不适用于独立于使用该属性的站点的软件。

class

The class attribute has several roles in HTML: As a style sheet selector (when an author wishes to assign style information to a set of elements). class属性在HTML中有几个角色:作为样式表选择器(当作者希望将样式信息分配给一组元素时)。 For general purpose processing by user agents. 用于通用用户代理处理。

When a certain area (HTML element) can have different views, but all the different views use the same data, then I believe it is more semantically "correct" to use a data attribute. 当某个区域(HTML元素)可以有不同的视图,但所有不同的视图使用相同的数据时,我相信使用数据属性在语义上更“正确”。

I normally use data-view , but if it's a loading state, then I would use a class, and create some generalized solution for all things with that loading class to look the same, if possible. 我通常使用data-view如果它是一个加载状态,那么我会使用一个类,并为所有事物创建一些通用解决方案,如果可能的话, loading类看起来相同。

A simple example, if you have a div which shows some data which is visually represented by some graph/chart and the user can switch the view of that area to become a " table " view, than that div would look like this: 一个简单的例子,如果你有一个div显示一些数据,这些数据可以通过某些图形/图表直观地表示,并且用户可以将该区域的视图切换为“ 表格 ”视图,那么div将如下所示:

default state 默认状态

<div class='myComponent' data-view='chart'>...</div>

loading state 装载状态

<div class='myComponent loading'>...</div>

alternative-view state 替代观点状态

<div class='myComponent' data-view='table'>...</div>

I use this style of coding extensively, and it can get nested. 我广泛使用这种编码风格,它可以嵌套。 for example, I usually work on SPA pages, where the content of the page changes depending on the URL, so the main HTML element might look like: 例如,我通常在SPA页面上工作,其页面内容根据URL而变化,因此main HTML元素可能如下所示:

<main id="page" data-view="home">

And the CSS: 而CSS:

#page[data-view="home"]{
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM