zoukankan      html  css  js  c++  java
  • data属性

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">

    </head>
    <body>
    <ul id="list"></ul>
    <div id="info"></div>
    <script type="text/javascript">
    var data = {
    01:{
    name:"张三1",
    age : 18
    },
    02:{
    name:"张三2",
    age : 14
    },
    03:{
    name:"张三3",
    age : 19
    },
    04:{
    name:"张三4",
    age : 17
    },
    }
    var list = document.getElementById("list");
    for (var id in data){
    var item = data[id];
    var liElement = document.createElement("li");
    liElement.appendChild(document.createTextNode(item.name));
    liElement.setAttribute("data-name",item.name);
    liElement.setAttribute("data-age",item.age);
    list.appendChild(liElement);
    liElement.addEventListener("click",function(e){
    console.log(e);
    // this 是当前点击的元素
    alert(this.name);
    alert(this.getAttribute('data-age'));
    console.log(this.dataset['age']);
    })
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    poj 2104(线段树)
    poj 1962(并查集+带权更新)
    hdu 2818(并查集,带权更新)
    hdu 1856
    hdu 3172
    hdu 1325(并查集)
    hdu 5023
    pku 2777(经典线段树染色问题)
    hdu 1671(字典树判断前缀)
    hdu 1247 (字典树入门)
  • 原文地址:https://www.cnblogs.com/wyq-web/p/9496237.html
Copyright © 2011-2022 走看看