Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么要使用html数据属性_Java_Html - Fatal编程技术网

Java 为什么要使用html数据属性

Java 为什么要使用html数据属性,java,html,Java,Html,让我们以这个标记为例 <div id="my_div" custom="some custom value"> <div id="sec_div" data-custom="other custom value"> <script> let div1 = document.getElementById("my_div"); let div2 = document.getElementById("sec_di

让我们以这个标记为例

    <div id="my_div" custom="some custom value">
    <div id="sec_div" data-custom="other custom value">

    <script>
       let div1 = document.getElementById("my_div");
       let div2 = document.getElementById("sec_div");
       console.log(div1.getAttribute("custom"))// outputs "some custom value"
       console.log(div2.dataset.custom)// outputs "other custom value"
    </script>

设div1=document.getElementById(“my_div”);
设div2=document.getElementById(“sec_div”);
console.log(div1.getAttribute(“custom”)//输出“一些自定义值”
console.log(div2.dataset.custom)//输出“其他自定义值”
同样,对于CSS,我们总是可以使用属性进行选择

 <style>
[data-custom]
{
/* Selects div with id "sec_div" */
}
[custom]
{
/* Selects div with id "my_div" */
}
</style>

[数据自定义]
{
/*选择id为“secu div”的div*/
}
[海关]
{
/*选择id为“my_div”的div*/
}

因此,我的问题是,为什么我们应该使用数据属性而不是直接编写组合属性名称使用数据属性不知何故是一种更好的方法

问题是什么?@Fenio为什么我们应该使用数据属性而不是直接编写组合属性名称,因为它们实际上是标准化的?