zoukankan      html  css  js  c++  java
  • 关于HTML5的data-*自定义属性的总结

    一、关于html元素的特性

    1.html元素都存在一些标准的特性:

        id 元素在文档中的唯一标识符;

        title 有关元素的附加说明信息,一般通过工具提示条显示出来;

        lang 元素内容的语言代码,很少使用;

        dir 语言的方向,很少使用;

        className,与元素的class特性对应,考虑到与ECMAScript的保留字class的冲突,因此命名为className;

    2.操作特性的方法:

        getAttribute() 获取相关特性,也可以获取到自定义的特性,区别于使用DOM元素的属性获取;   

        setAttribute() 设置相关特性; 

        removeAttribute() 删除相关特性;

    3.关于获取html元素特性中使用getAttribute()与直接使用DOM元素的属性获取的区别 :

    例如:

    var header=document.getElementById('header');
    console.log(header.id);//header
    console.log(header.getAttribute('id'));//header
    console.log(header.title);//test
    console.log(header.getAttribute('title'));//test
    console.log(header.myTitle);//undefined
    console.log(header.getAttribute('myTitle'));//monster1935
    

    在本文示例中演示了通过两种方法获取html元素的标准特性以及自定义特性。可以发现: 

    getAttribute()既可以访问标准特性也可以访问自定义特性,而是用DOM元素本身属性访问html元素特性的时候只能访问标准特性。 

    原因:

    html元素中只有标准特性才会以属性的形式添加到DOM对象中。

    特殊情况:

    有两类特性,虽有在DOM对象中有对应的属性名,但是属性的值与通过getAttribute()返回的值并不相同。

    1.style特性:

    使用DOM对象属性访问style的时候,返回的是一个对象,而使用getAttribute()方法获取的时候返回的是css文本。

    2.事件处理程序(例如:onclick):

    使用DOM对象的属性访问的时候,返回的是一个JavaScript函数,而通过getAttribute()方法获取时返回的是javas代码。

    使用方法:

    只有在获取自定义特性的时候使用getAttribute()方法,操作DOM的时候,使用对象的属性来获取。

    二、Html5中的自定义属性

    1.Html5规范中规定自定义属性需要添加前缀data-,目的是提供与渲染无关的信息。

    2.使用getAttribute()方法以及setAttribute()方法操作自定义属性。 

    例如:

    var user=document.getElementById('user');
    //使用attributes方法操作属性
    //获取相关属性值
    var name=user.getAttribute('data-name');
    console.log(name)//monster1935
    var age = user.getAttribute('data-age');
    console.log(age);//123
    //设置相关属性值
    user.setAttribute('data-sex','male');
    console.log(user.getAttribute('data-sex'));//male
    

    上述示例中展示了使用getAttribute()以及setAttribute()方法操作html元素的自定义属性。

    3.使用dataset操作自定义属性 

    例如:

    var el=document.querySelector('#user');
    console.log(el.id);
    console.log(el.dataset); //返回的是一个DomStringMap对象
    console.log(el.dataset.name);
    console.log(el.dataset.age);
    // 设置相关属性
    el.dataset.home="shandong";
    console.log(el.dataset);
    // 删除相关属性
    // delete el.dataset.home;
    el.dataset.home = null;
    console.log(el.dataset);

    以上示例展示了使用dataset属性来操作自定义属性。dataset属性的值是一个DOMStringMap的一个示例,是一个名值对的映射。在这个映射中,每个data-name的形式的属性都有一个对应的属性,不同的是属性名中没有data-前缀。(比如,自定义属性是data-name,映射中对应的属性就是name).还有一点需要注意的是如果data-属性名中包含了连字符(比如:data-date-of-birth),则映射中对应的属性为dateOfBirth,即转换为相应的驼峰格式来显示。

    4.dataset属性的兼容性问题 

    Chrome 8+ Firefox(Gecko) 6.0+ Internet Explorer 11+ Opera 11.10+ Safari 6+

    5.使用data-属性选择器

    1.获取相关DOM元素

    var elem=document.querySelectorAll("[data-name='monster1935']");

    2.设置相关css样式

    <style>
      div{
        100px;
        height:200px;
        margin:20px;
      }
      div[data-name="monster"]{
        background-color: green;
      }
      div[data-name="monster1935"]{
        background-color: red
      }
    </style>
  • 相关阅读:
    xls与csv文件的区别
    青音,经典爱情语录
    win7用户账户自动登录方法汇总
    How to using Procedure found Lead Blocker
    FTS(3) BSD 库函数手册 遍历文件夹(二)
    FTS(3) BSD 库函数手册 遍历文件夹(一)
    DisplayMetrics类 获取手机显示屏的基本信息 包括尺寸、密度、字体缩放等信息
    About App Distribution 关于应用发布
    FTS(3) 遍历文件夹实例
    OpenCV 2.1.0 with Visual Studio 2008
  • 原文地址:https://www.cnblogs.com/zaixiachengxuyuan/p/14465565.html
Copyright © 2011-2022 走看看