dataset最基本使用实例页面

代码:

HTML代码:
<div id="day2-meal-expense" data-drink="coffee" data-food="sushi" data-meal="lunch">¥20.12</div>
<button id="button">点击我</button>
                
JS代码:
var expenseday2 = document.getElementById("day2-meal-expense");  
var typeOfDrink = expenseday2.dataset && expenseday2.dataset.drink;
document.getElementById("button").onclick = function() {
    alert("饮料是:" + typeOfDrink);
};
                

效果:

¥20.12