在某些情况下,由于超出此问题范围的原因, anchor 会重复。这是一个示例 div:
<div class="unifyRepeat listing">
<a class="businessAnchor" name="abcchildcareandlearningcenter"></a>
<a class="businessAnchor" name="abcchildcareandlearningcenter"></a>
<a class="businessAnchor" name="abcchildcareandlearningcenter"></a>
<a class="businessAnchor mceItemAnchor" name="abcchildcareandlearningcenter"></a>
<table class="tblListing">
<tbody>
<tr>
<td>
<p class="bold">ABC Child Care and Learning Center</p>
<p>Jane Smith</p>
<p>(555) 555-1234</p>
</td>
<td>
<img alt="" src="images/ABCchildcare.jpg">
</td>
<td>
</td>
</tr>
</tbody>
</table>
<a class="linkToTop" href="#top">^ Top</a>
</div>
这是附加 anchor 并尝试删除任何现有 anchor 的 jQuery:
$('#businessListings div.listing').each(function() {
var name = $(this).find('p:first').text(),
cleanedName = name.replace(/\W/g, '').toLowerCase();
$('ul#businessListingLinks').append('<li><a href="#' + cleanedName + '">' + name + '</a>');
var anchor = '<a class="businessAnchor" name="' + cleanedName + '"></a>';
$(this).remove('a.businessAnchor');
//$(this).each('a.businessAnchor').remove();
$(this).prepend(anchor);
});
我认为remove()行会选择“businessAnchor”类的所有 anchor 标记,但事实并非如此。
正如你所看到的,我尝试了each()函数,但是没有用。我不确定这是因为我没有正确实现它,还是其他原因。
最佳答案
试试这个:
$(this).find('a.businessAnchor').remove();
从标记来看,a.businessAnchor 似乎是 div 的直接子级,因此您也可以这样做:
$(this).children('a.businessAnchor').remove();
关于jQuery - 从 div 中删除具有特定类的所有 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16146910/