JavaScript Replace in Class
I need to remove a period inside of a div that only has a class attribute (no id -- system g开发者_运维问答enerated page).
<div class="myClass">
Some text .
</div>
I need to remove the period. How can this be accomplished using JavaScript?
Without jQuery, here is the old-school option:
var divs = document.getElementsByTagName('div');
for (var i=0,len=divs.length;i<len;++i){
if (/(?:^|\s)myClass(?:\s|$)/.test(divs[i].className)){
// Removes every period
divs[i].innerHTML = divs[i].innerHTML.replace( /\./g, '' );
}
}
With jQuery:
$('div.myClass').html(function(i,oldHTML){
return oldHTML.replace( /\./g, '' );
});
If you're using jQuery, you can do this:
$(".myClass").html(function(i, html) { return html.replace(/\.$/, ""); });
精彩评论