我正在尝试获取单击的锚标记的href,并将类添加到具有相同href的所有元素?我知道如何向一个元素添加类,但是如何向具有相同href的所有元素添加类?$('.shop-item').on('click', function(e){ var hrefz = $(this).attr('href'); //get the href on the clicked anchor tag var otherElem = $('a[href$='+hrefz+']').attr('href'); alert(otherElem); //$(this).addClass('newClass');});编辑:HTML(这是hrefs的方式)<ul class="shop-list"> <li class="shop-item"><a href="#Ordering"></a></li> <li class="shop-item"><a href="#Delivery"></a></li> ....</ul>
1 回答

慕桂英3389331
TA贡献2036条经验 获得超8个赞
只需调用addClass
您选择的集合:
$('a[href='+hrefz+']').addClass('newClass');
请注意,href$=
将选择以-结尾的href
s-如果要选择完全匹配的s ,则应改用。hrefz
href
href=
添加回答
举报
0/150
提交
取消