for (var i = 0; i <= 5; i++) { const divJoinDay = document.createElement("div"); divJoinDay.style.padding = "6px 30px"; divJoinDay.style.backgroundColor = "#ffffff"; divJoinDay.style.color = "#000"; divJoinDay.style.borderBottom = "solid 1px #e6e6e6"; if (i === 0) { divJoinDay.innerHTML = "待 选"; } else { divJoinDay.innerHTML = "第 "+ i +" 天"; } divJoinDay.setAttribute("data-day", i); divJoinDay.onmouseover = (e) => { divJoinDay.style.backgroundColor = "#f39800"; }; divJoinDay.onmouseout = (e) => { divJoinDay.style.backgroundColor = "#ffffff"; }; divJoinDay.onclick = (e) => { const dayId = e.target.getAttribute("data-day"); alert("第 "+ dayId +" 天"); // 阻止冒泡事件 e.cancelBubble = true; }; divPullDown.appendChild(divJoinDay); }
设置值:
document.setAttribute("data-day", i);
点击事件获取值
e.target.getAttribute("data-day");
原文地址:https://www.cnblogs.com/liugx/p/9559452.html
时间: 2024-10-24 02:45:10