Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript更改外部SVG文本_Javascript_Svg - Fatal编程技术网

使用javascript更改外部SVG文本

使用javascript更改外部SVG文本,javascript,svg,Javascript,Svg,我在更改外部SVG中的文本时遇到问题。我找到了这里列出的许多解决方案,但没有一个有效 添加侦听器: document.getElementById('id1').addEventListener('onload', func); 职能: function func () { var ele = document.getElementById('id1'), svg = ele.contentDocument, p = svg.getElementByI

我在更改外部SVG中的文本时遇到问题。我找到了这里列出的许多解决方案,但没有一个有效

添加侦听器:

document.getElementById('id1').addEventListener('onload', func);
职能:

function func () {

    var ele = document.getElementById('id1'),
        svg = ele.contentDocument,
        p = svg.getElementById('id2');

    alert(p.textContent);
    p.textContent = 'test';

}
第一次加载时,p返回null。在第二次加载时,警报将返回SVG文本元素中的正确文本,但该文本不会被下一条语句更新


为什么它在第一次加载时不起作用,为什么文本没有变化

文本元素是可见的,并且其中包含默认文本,因此我认为x或y值不会出现问题。我试着添加它们,但似乎没有任何效果。我希望警报最初返回默认文本。请改用innerHTML。IE8或更早版本不支持TextContentText元素是可见的,并且其中包含默认文本,因此我认为x或y值不会有问题。我试着添加它们,但似乎没有任何效果。我希望警报最初返回默认文本。请改用innerHTML。IE8或更早版本不支持文本内容“id1”和“id2”是什么类型的元素?您可以创建一个工作代码段,或者至少添加相关的HTML和SVG。什么类型的元素是“id1”和“id2”?您可以创建一个工作代码段,或者至少添加相关的HTML和SVG。
<svg height="90" width="200">
  <text 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>
  <text>will not display as x and y are missing</text>
</svg>
p.textContent = 'test';
alert(p.textContent);