一个HTML“数据”元素中可以有多个值吗?

一个 HTML“数据”元素中可以有多个值吗?类似于一个类可以有多个类名.

如果可能,我想创建一个使用一个“data-”元素来容纳所有库样式的CSS / JS库.例如:

<div data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>

这样,任何程序员的习惯风格规则都可以进入元素类.我的理由是使可读性更容易,所以一起看起来就像:

<div class="custom-style another-style" data-library-name="xs-hidden col-md-10 col-xl-8 big-hero"></div>

解决方法

Can I have multiple values in one HTML “data-” element?

你可以有一个字符串.该规范没有为属性中的数据定义任何特定格式,该格式旨在由特定于站点的JavaScript进行处理.

Similar to how a class can have multiple class names.

class属性使用空格分隔的类列表.

您的JavaScript可以your_data_attribute_value.split(“”);如果你喜欢.

用CSS处理这个会使用〜= attribute selector.

[att~=val] Represents an element with the att attribute whose value is a whitespace-separated list of words,one of which is exactly “val”. If “val” contains whitespace,it will never represent anything (since the words are separated by spaces). Also if “val” is the empty string,it will never represent anything.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些