开发者

JavaScript Replace in Class

开发者 https://www.devze.com 2023-02-09 03:09 出处:网络
I need to remove a period inside of a div that only has a class attribute (no id -- system g开发者_运维问答enerated page).

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

精彩评论

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