我正在嘗試獲取單擊的錨標記的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>
將類添加到具有相同href的所有元素
慕容3067478
2021-04-30 14:11:00