开发者

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(/\.$/, ""); });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜