<style>
.red {
color: red;
}
</style>
<div class="layui-form-mid">
[ <a href="javascript:void(0);" data-type="2" class="getReportDate red">今日报表</a>]
[ <a href="javascript:void(0);" data-type="1" class="getReportDate">昨日报表</a> ]
[ <a href="javascript:void(0);" data-type="3" class="getReportDate">本周报表</a>]
[ <a href="javascript:void(0);" data-type="6" class="getReportDate">上周报表</a>]
[ <a href="javascript:void(0);" data-type="4" class="getReportDate">本月</a> ]
[ <a href="javascript:void(0);" data-type="5" class="getReportDate">上月</a>]
</div>
<div class="layui-form-mid">
[ <a href="javascript:void(0);" data-type="2" class="getReportDate red">今日报表</a>]
[ <a href="javascript:void(0);" data-type="1" class="getReportDate">昨日报表</a> ]
[ <a href="javascript:void(0);" data-type="3" class="getReportDate">本周报表</a>]
[ <a href="javascript:void(0);" data-type="6" class="getReportDate">上周报表</a>]
[ <a href="javascript:void(0);" data-type="4" class="getReportDate">本月</a> ]
[ <a href="javascript:void(0);" data-type="5" class="getReportDate">上月</a>]
</div>
根据class出发事件 获取点击的data 值, 遍历所有 getReportDate 类 下面的 a 标签的data 的值,如果遍历的值和点击的值相等,那么给a 标签添加样式 red ,并且移除之前的 red 类属性
//时间报表
$(document).on('click', '.getReportDate', function () {
$("a").removeClass("red");
var type = $(this).data("type");
$(".getReportDate").each(function (i, item) {
//遍历获取所有的 getReportDate class 下面所有的a 标签
var data = $(item).data("type");
if(type==data){
$(item).addClass("red")
}
});
});