要在 svg 的文本元素中保留空格,应使用 'xml:space="preserve"' 作为文本的属性(jsfiddle)。但是,它不起作用。我究竟做错了什么?
// 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);
你快到了。您需要在 xml 命名空间中正确创建需要 setAttributeNS 而不是 setAttribute 的属性
text.node.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)