如何在SVG中通过TypeScript设置TextArea.innerHTML



这是我尝试使用TypeScript重新创建的一些SVG代码。

<svg>
<switch>
<g requiredFeatures="http://www.w3.org/Graphics/SVG/feature/1.2/#TextFlow">
<textArea width="200" height="auto">
Text goes here
</textArea>
</g>
<foreignObject width="200" height="200" 
requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility">
<p xmlns="http://www.w3.org/1999/xhtml">Text goes here</p>
</foreignObject>
<text x="20" y="20">No automatic linewrapping.</text>
</switch>

在大多数情况下,我都有这个工作,但是我不确定如何将文本插入文本区域(和文本(元素。这是我想做的:

const textArea = document.createElementNS(this.xmlns, 'textArea');
textArea.setAttributeNS(null, 'width', '200');
textArea.setAttributeNS(null, 'height', 'auto');
//textArea.setAttributeNS(null, 'value', text);
textArea.innerHTML('Text goes here');

TypeScript 根本不喜欢 innerHTML 语句。它给了我错误:无法调用类型缺少调用签名的表达式。类型"字符串"没有兼容的呼叫签名。

我不确定这意味着什么,我该如何解决?我在某处看到一个帖子,说只为文本设置"值",我尝试过,但在这种情况下它不显示文本,它只是变成了另一个参数。

提前感谢您提供的任何建议。

innerHTML是一个string属性(不是函数(,所以你可以这样设置它:

textArea.innerHTML = 'Text goes here';

演示

最新更新