从一个div中选择数据属性,将类添加到具有相同数据属性值的另一个div

我有2个div具有相同的数据属性,如下所示:

$("#devMap div").click(function(){ var data = $(this).attr('data-name'); alert(data); //this is getting the correct data-name value if($('#accordian .blocks').data('name')===data){ $(this).toggleClass('active'); // i want this to add the class active to the #accordian .blocks which has the data-name equal to the #devMap div data-name value } 
  

我希望当用户点击on #devMap .toggleAccordian它会切换on #devMap .toggleAccordian下相同数据名属性的#accordian ,我试过这个:

您可以使用属性相等选择器来获取属性data-name等于当前数据名称的所有.block元素:

 $('#accordian .blocks[data-name="'+data+'"]').toggleClass('active');