html中的标签都可以加一个id的属性。
<body> <div id="tree" data-leaves="47" data-plant-height="2.4m"></div> <script> var tree = document.getElementById("tree"); //getAttribute()取值属性 console.log(tree.getAttribute("data-leaves")); console.log(tree.getAttribute("data-plant-height")); //setAttribute()赋值属性 tree.setAttribute("data-leaves","48"); //data-前缀属性可以在JS中通过dataset取值,更加方便 console.log(tree.dataset.leaves); console.log(tree.dataset.plantHeight); //赋值 tree.dataset.plantHeight = "3m"; tree.dataset.leaves--; //新增data属性 tree.dataset.age = "100"; //删除,设置成null,或者delete tree.dataset.leaves = null; delete tree.dataset.age; //jQuery的data方法 var tree = $('#tree'); console.log(tree.data("plant-height")); </script> </body>
绑定点击事件
<a id="sam" class="gray_btn" href="javascript:void(0)" onclick="rClick({$vo.lsn_begin_time},{$vo.id})" data-time= "{$vo.lsn_begin_time}" data-id ="{$vo.id}">修改教材</a> <script> function rClick(begin_time,id){ console.log(begin_time); console.log(id); // var tree = document.getElementById("sam"); // //上课时间 // var begin_time = tree.getAttribute("data-time"); // //课时id // var id = tree.getAttribute("data-id"); // var myDate = new Date();//获取系统当前时间 //当前时间 js获取时间 得到是毫秒 //var time_now = (new Date().getTime()); //var timestamp =(new Date()).valueOf(); begin_time2 = begin_time * 1000; // console.log(begin_time) // console.log(time_now) // console.log(begin_time-time_now); // console.log(id); if(begin_time2-time_now < 7200000){ alert('开课前2小时内无法修改章节,下次请提前操作!'); }else{ window.location.href="__URL__/updateLessonMaterial/id/"+id; } } </script>