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