JavaScript not able to replace String with Number of same words

时间:2016-04-07 10:41:51

标签: javascript

I am trying to replace a particular word in String. This word has repeated many times. this is my String:($scope.selectedText)

 <span style="font-size:20px>This is some text</span>
<span style="font-size:30px>This is some text</span>
<span style="font-size:25px>This is some text</span>

I am trying to replace font-size like this:

  var font = "font-size:"+15+".000";
  var formatting = $scope.selectedText.replace("font-size:",font);

But this ends in just changing the first fonr-size i.e

 <span style="font-size:15px>This is some text</span>
    <span style="font-size:30px>This is some text</span>
    <span style="font-size:25px>This is some text</span>

Can i know where i am doing it wrong?? I know we can do this programitically , But this is an execption! ThankYou.

1 个答案:

答案 0 :(得分:1)

You can do it this way:

var elements = document.getElementsByTagName("span");

for(var i = 0; i < elements.length; i++) {
    elements[i].style.fontSize = 15 + "px";
}

there is a missing closing quote mark in your markup:

<span style="font-size:20px">This is some text</span>
<span style="font-size:30px">This is some text</span>
<span style="font-size:25px">This is some text</span>

http://codepen.io/ingvi/pen/EKoyrj