4
在某些情况下,由于超出此问题范围的原因,锚点正在重复。下面是一个示例的div:jQuery - 从一个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>
下面是追加锚,并试图删除任何现有的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);
});
我认为删除()行会选择所有锚标签班级“businessAnchor”,但事实并非如此。你可以看到,我尝试了each()函数,但是这并不起作用。我不确定是否因为我没有正确实施或者其他原因。
我想你应该尝试停止脚本生成重复,而不是仅仅删除它们。使用'.remove()'*应该*删除jQuery对象中的所有元素,也许尝试在*之前添加元素*。 – Bill 2013-04-22 12:19:46
'$(this).find('a.businessAnchor')。remove();' – undefined 2013-04-22 12:20:58
ID是哪里?你调用'#businessListings'但它不在你提供的html中# – 2013-04-22 12:21:11