我已经在浏览器上花费了两个小时,试图找到一个好的答案来解决一个看起来很多人都遇到的问题,但我找不到一个合适的解决方案来解决我的问题。我需要改变svg文件中的文本,最终可能是浏览器中的文本输入。我设法使用foreignobject,但最终这不是我的问题的解决方案,因为我需要将其与路径对齐。
我的SVG
我的SVG
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 20.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 841.9 595.3" style="enable-background:new 0 0 841.9 595.3;" xml:space="preserve">
<style type="text/css">
</style>
<text transform="matrix(1 0 0 1 325.2451 144.7144)" class="st0 st1" id="id-of-the-text">hey</text>
<text class="testText" id="testText" x="10" y="20" style="fill:red;">Several lines:
<tspan x="10" y="45">First line.</tspan>
<tspan x="10" y="70">Second line.</tspan>
</text>
<text x="40" y="60">more text</text>
</svg>
以及对类似问题的答案的失败尝试
$('#id-of-the-text').textContent = 'test';
$("#id-of-the-text").text("new-value");
$("#id-of-the-text")['innerText' in $("#id-of-the-text") ? "innerText" : "textContent"] = "some value";
可能是一个愚蠢的小错误,否则我就不明白为什么什么都对我没用。
id-of-text
,而不是testText
。使用$('#id-of-the-text').text('New Text');
可以起作用:https://jsfiddle.net/5gfcym6p/ - Yuriy Galanter