Dynamically change text of SVG file

2019-08-15 03:15发布

I have external SVG file which contains the code

<g
     inkscape:groupmode="layer"
     id="layer9"
     inkscape:label="score"
     style="display:inline">
     <text
        xml:space="preserve"
        style="font-style:normal;font-weight:normal;font-size:22.5px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
        x="100.3568906"
        y="20.353357"
          id="text5833"
        sodipodi:linespacing="125%"><tspan
          sodipodi:role="line"
          id="tspan5834"
          x="300.3568906"
          y="20.353357">Score</tspan></text>

  </g>

I need to dynamically change the text Score from JS file, i already tried but not able to change the text dynamically. what i tried is:-

var list = layerNamed('score').getElementsByTagName("g");
var textNode = document.createTextNode("Score:-1");
list.appendChild(textNode);

3条回答
姐就是有狂的资本
2楼-- · 2019-08-15 04:03

For your specific example, here's how to set the text using both raw JavaScript and jQuery:

updateText("tspan5834", "A different score");
//updateTextWithJQuery("tspan5834", "now using jquery"); // uncomment this line to see how jquery works

function updateText(tspanId, txt) {
  var spanEl = document.getElementById(tspanId);
  while( spanEl.firstChild ) {
    spanEl.removeChild( spanEl.firstChild );
  }
  spanEl.appendChild( document.createTextNode(txt) );
}

function updateTextWithJQuery(tspanId, txt) {
    $("#"+tspanId).text(txt);
}

jsFiddle: https://jsfiddle.net/w91sn1dk/3/

查看更多
不美不萌又怎样
3楼-- · 2019-08-15 04:10

You can use the following code to change the text inside existing text element.

document.getElementById('textid').textContent = "new text";

Working example below:

function changeText()
{
  document.getElementById('textid').textContent = "new text";
}
<svg height="30" width="200">
  <text id="textid" x="0" y="15" fill="red">I love SVG!</text>
  Sorry, your browser does not support inline SVG.
</svg>

<button onclick="changeText()">Click here to change text</button>

查看更多
We Are One
4楼-- · 2019-08-15 04:14

There are a few problems in your code.

1) layerNamed('score').getElementsByTagName("g") returns a collection of all elements with the specified tag name, as a NodeList object. The nodes can be accessed by index numbers. The index starts at 0.

So to access the first g element, you should code as shown below.

var list = layerNamed('score').getElementsByTagName("g")[0];

2) There is no need for appending a new text element for updating the text content. Just access the text and tspan and update the text content of tspan.

var textNode = list.getElementsByTagName("text")[0].children[0];
textNode.textContent = "New Text";

var list = document.getElementsByTagName("g")[0]; //document.getElementById("#layer9");
var textNode = list.getElementsByTagName("text")[0].children[0];
textNode.textContent = "New Text";
<svg height="30" width="800">
<g inkscape:groupmode="layer" id="layer9" inkscape:label="score" style="display:inline">
     <text xml:space="preserve" style="font-style:normal;font-weight:normal;font-size:22.5px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" x="100.3568906" y="20.353357" id="text5833" sodipodi:linespacing="125%"><tspan sodipodi:role="line" id="tspan5834" x="300.3568906" y="20.353357">Hello</tspan></text>

  </g>
</svg>

查看更多
登录 后发表回答