您可以使用一点javascript,使用getComputedTextLength()方法来计算每个文本的位置。
let h = hello.getComputedTextLength(); time.setAttributeNS(null,"x", 10 + h + 20 ) let t = time.getComputedTextLength(); dfsdf.setAttributeNS(null,"x", 10 + h + 20 + t + 20 )
HelloHellodfsdfdfsdf1/1/2018 4:00:00 AM1/1/2018 4:00:00 AMEarliest Date Time EndEarliest Date Time End
另一种解决方案是将文本定位如下:
您好 : text-anchor="start" x="10"
dfsdf : text-anchor="end" x="490"
时间 : text-anchor="middle" x="250"
HelloHellodfsdfdfsdf1/1/2018 4:00:00 AM1/1/2018 4:00:00 AMEarliest Date Time EndEarliest Date Time End