在HTML标签中定义属性
在HTML标签中界说属性,需要时可以读取属性的值,这类属性称为数据属性
界说数据属性定名法则是data-<属性名称>
例如
<div id="message" data-text="hello,world" ></div> <button onclick="read()">submit</button> 在JavaScript中读取读取时通过元素的dataset.<属性名称>访谒,
function read(){ obj = document.getElementById('message'); console.log(obj.dataset.text); }这样触发函数就能读取data-text的值
HTML数据属性 data-*
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32880.html