74,页面多个a标签,第个标签的data值都不一样。JQ如何获取同一标签内的元素的值。



 <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")
          }
        });

       
    });







最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。