options.onclick = function ({target:{dataset:{type}}}) { // 结构出自定义属性data-type,type为字符串 optionItems.forEach((item, index) => { if (index === +type) { // 遍历出的option的data-type和index一致则改变背景色,反之设置白色 // 同时把对应索引下的item显示页面,反之隐藏 item.style.backgroundColor = "#25bb9b"; items[index].style.display = "block"; } else { item.style.backgroundColor = "#fff"; items[index].style.display = "none"; } }); };