您有重复的ID,这是无效的,并且还有jQueryID选择器(或jQuery内部使用的任何其他ID选择器,例如document.getElementById,因为ID元素在大多数浏览器中都是索引的,并且本来是唯一的)将仅返回第一个出现在DOM中。将其更改为类,并查看其工作情况:
$('.comment').each(function() {
var thz = $(this); var repl =
thz.html(thz.html().replace(/\D+/g, ''));
});
的HTML
<a class="comment1" href="#"> c2f?f011. </a>
<a class="comment1" href="#">c20ff113. </a>
<a class="comment1" href="#"> c201gf76341. </a>
顺便说一下,你的身份证是这样的:
<a id="comment1" href="#"> c2f?f011. </a>
<a id="comment2" href="#">c20ff113. </a>
<a id="comment3" href="#"> c201gf76341. </a>
以“属性选择器”开头将为您提供帮助(但实际上会使您减速,因为这是一个属性选择器,并且失去了使用ID的优势)。
$('[id^=comment]').each(function() { // While using this better give a container context $('[id^=comment]', 'container').each(function...
var thz = $(this);
var repl = thz.html(thz.html().replace(/\D+/g, ''));
});