简体   繁体   English

如何在javascript的svg对象中添加文本

[英]how to add text in svg object inside javascript

How can I add a text node inside javascript svg object . 如何在javascript svg对象内添加文本节点。 I am using this object , but I don't know how to add text into svg . 我正在使用此对象,但不知道如何在svg中添加文本。

 var circlemarker = {        
        path: 'M-5,0a5,5 0 1,0 10,0a5,5 0 1,0 -10,0 z',
        fillColor:'yellow', //'#f39c13',
        fillOpacity: 0.8,
        scale: 3,
        strokeColor: '#f7692c',
        strokeWeight: 2,
        data: 'My text' // this is not working
    };

First of all, read more about SVG here 首先, 在此处阅读有关SVG的更多信息

TL;DR TL; DR
SVG is just set of elements that represent a geometric figure. SVG只是代表几何图形的一组元素。 Simple SVG looks like 简单的SVG看起来像

<svg width="100" height="100">
   <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
   <text x="30" y="40">aaa</text>
</svg> 

Notice that <text></text> is an element, so it cannot be used inside <circle></circle> but underneath 请注意, <text></text>是一个元素,因此不能在<circle></circle>内部使用,但要在下面使用

So to add text, create new text element like this 因此,要添加文本,请像这样创建新的文本元素

var textmarker = {
    x: 10,
    y: 40,
    fontSize: 10,
    fontFamily: 'Verdana',
    value: 'some text' //this might be tricky, as per docs, text element filled by innerHTML property, try data, text, innerHTML, etc
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM