【问题标题】:Dynamically change text of SVG file动态更改 SVG 文件的文本
【发布时间】:2016-02-09 09:31:46
【问题描述】:

我有包含代码的外部 SVG 文件

<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>

我需要动态更改 JS 文件中的文本分数,我已经尝试过但无法动态更改文本。 我尝试的是:-

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

【问题讨论】:

  • 不相关,但如果您要广泛使用 svg,请尝试 d3.js。
  • @Robert Longson 我也尝试使用文本但没有用..

标签: javascript css svg


【解决方案1】:

您可以使用以下代码更改现有 text 元素内的文本。

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

下面的工作示例:

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>

【讨论】:

    【解决方案2】:

    您的代码中存在一些问题。

    1) layerNamed('score').getElementsByTagName("g") 返回具有指定标签名称的所有元素的集合,作为 NodeList 对象。可以通过索引号访问节点。索引从 0 开始。

    所以要访问第一个 g 元素,您应该编写如下所示的代码。

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

    2) 不需要添加新的文本元素来更新文本内容。只需访问 text 和 tspan 并更新 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>

    【讨论】:

      【解决方案3】:

      对于您的具体示例,以下是使用原始 JavaScript 和 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/

      【讨论】:

        猜你喜欢
        • 2011-11-14
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-26
        相关资源
        最近更新 更多