我想删除标题末尾的冒号,所以我用它:
$('.title').html($('.title').html().replace(':', ''))Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="title">
Apples:
</div>
<div class="title">
Oranges:
</div>Run Code Online (Sandbox Code Playgroud)
它的工作原理,它消除了冒号,但它也取代了第二个冠军头衔Oranges与Apples?
您可以给出html一个闭包并相互独立地更改每个闭包.
$('.title').html(function(index, currentHTML){
return currentHTML.replace(/:/g, '');
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="title">
Apples:
</div>
<div class="title">
Oranges:
</div>Run Code Online (Sandbox Code Playgroud)