在createElementNS()之后更改字体颜色

Change font color after createElementNS()

本文关键字:字体 颜色 之后 createElementNS      更新时间:2023-09-26

im构建一个游戏,并尝试在游戏结束时显示高分。当前用户名应该用不同的颜色突出显示,但我无法使用它。当我试图更改字体大小以进行测试时,它仍然有效!

var nameSpan = svgdoc.createElementNS("http://www.w3.org/2000/svg", "tspan");
nameSpan.setAttribute("x", 130);
nameSpan.setAttribute("dy", 30);
var nameTextNode = svgdoc.createTextNode(record.name);
if (record.name == playerName){
    nameSpan.style.fontColor = "red"; // does not work
    nameSpan.style.color = "blue"; // does not work
    nameSpan.style.fontSize = "100px"; // works fine
}
nameSpan.appendChild(nameTextNode);

我尝试了不同的方法来应用颜色(上面可以看到其中的两种),但它一直保持黑色。

SVG文本不使用颜色着色。相反,它使用填充和笔划,这样你就可以分别为轮廓上色。将内容更改为nameSpan.style.fill = "blue",它应该对您有效。