Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/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
如何在svg文本中保留空格_Svg_Snap.svg - Fatal编程技术网

如何在svg文本中保留空格

如何在svg文本中保留空格,svg,snap.svg,Svg,Snap.svg,要在svg的textelement中保留空格,应使用“xml:space=“preserve”作为文本的属性。然而,它不起作用。我做错了什么 // init snap var svgElement=document.getElementById("mainSvgId"); var s = Snap(svgElement).attr({height: 300, width: 300}); // greate group with rectanle var

要在svg的textelement中保留空格,应使用“xml:space=“preserve”作为文本的属性。然而,它不起作用。我做错了什么

    // init snap
    var svgElement=document.getElementById("mainSvgId");
    var s = Snap(svgElement).attr({height: 300, width: 300});

    // greate group with rectanle
    var parentGroup=s.g().attr({id: "parent"});
    var rect1 = s.rect(0, 0, 200, 200).attr({fill: "#bada55"});
    parentGroup.add(rect1);

    // add text with preserve attribute
    var text = s.text(0, 20, "   text1    text2");
    text.node.setAttribute("xml:space", "preserve");
    parentGroup.add(text);

你快到了。您需要在需要setAttributes而不是setAttribute的xml命名空间中正确创建属性

text.node.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
这显然是错误的。你应该现在就用。