如何使用Javascript在圆形SVG中添加文本?

如何使用Javascript在圆形SVG中添加文本?,svg,Svg,预期结果使用Javascript在圆圈内添加1 const svg=document.querySelector('#svg'); const svgNS=svg.namespaceURI; const rect=document.createElements(svgNS,'rect'); 常数offsetX=20; 常数偏移=20; const outerCircle=document.createElements(svg.namespaceURI,'circle'); setAttrib

预期结果使用Javascript在圆圈内添加
1

const svg=document.querySelector('#svg');
const svgNS=svg.namespaceURI;
const rect=document.createElements(svgNS,'rect');
常数offsetX=20;
常数偏移=20;
const outerCircle=document.createElements(svg.namespaceURI,'circle');
setAttributeNS(null,'cx',offsetX.toString());
setAttributeNS(null,'cy',offsetY.toString());
setAttributeNS(null,'r','9');
setAttributeNS(null,'fill','white');
setAttributeNS(null,'stroke','white');
setAttributeNS(null,'笔划宽度','1');
svg.appendChild(outerCircle);
const circle=document.createElements(svg.namespaceURI,'circle');
circle.setAttributeNS(null,'cx',offsetX.toString());
circle.setAttributeNS(null,'cy',offsetY.toString());
圆.setAttributeNS(null,'r','7');
circle.setAttributeNS(null,'fill','red');
circle.setAttributeNS(null,'stroke','red');
circle.setAttributeNS(null,'笔划宽度','1');
svg.appendChild(圆);
const number=document.createElements(svg.namespaceURI,'text');
number.setAttributeNS(null,'x',offsetX.toString());
number.setAttributeNS(null,'y',offsetY.toString());
setAttributeNS(null,'text-anchor','middle');
setAttributeNS(null,'stroke','white');
setAttributeNS(空,'笔划宽度','1px');
svg.appendChild(数字)
svg{
边框:1px实心#000000;
背景:黑色;
}

与向DOM元素添加文本的方法相同,
DOM.textContent='1'

const svg=document.querySelector('#svg');
const svgNS=svg.namespaceURI;
const rect=document.createElements(svgNS,'rect');
常数offsetX=20;
常数偏移=20;
const outerCircle=document.createElements(svg.namespaceURI,'circle');
setAttributeNS(null,'cx',offsetX.toString());
setAttributeNS(null,'cy',offsetY.toString());
setAttributeNS(null,'r','9');
setAttributeNS(null,'fill','white');
setAttributeNS(null,'stroke','white');
setAttributeNS(null,'笔划宽度','1');
svg.appendChild(outerCircle);
const circle=document.createElements(svg.namespaceURI,'circle');
circle.setAttributeNS(null,'cx',offsetX.toString());
circle.setAttributeNS(null,'cy',offsetY.toString());
圆.setAttributeNS(null,'r','7');
circle.setAttributeNS(null,'fill','red');
circle.setAttributeNS(null,'stroke','red');
circle.setAttributeNS(null,'笔划宽度','1');
svg.appendChild(圆);
const number=document.createElements(svg.namespaceURI,'text');
number.setAttributeNS(null,'x',offsetX.toString());
number.setAttributeNS(null,'y',offsetY.toString());
setAttributeNS(null,'text-anchor','middle');
setAttributeNS(null,'stroke','white');
setAttributeNS(空,'笔划宽度','1px');
number.textContent='1';
svg.appendChild(数字)
svg{
边框:1px实心#000000;
背景:黑色;
}

与向DOM元素添加文本的方法相同,
DOM.textContent='1'

const svg=document.querySelector('#svg');
const svgNS=svg.namespaceURI;
const rect=document.createElements(svgNS,'rect');
常数offsetX=20;
常数偏移=20;
const outerCircle=document.createElements(svg.namespaceURI,'circle');
setAttributeNS(null,'cx',offsetX.toString());
setAttributeNS(null,'cy',offsetY.toString());
setAttributeNS(null,'r','9');
setAttributeNS(null,'fill','white');
setAttributeNS(null,'stroke','white');
setAttributeNS(null,'笔划宽度','1');
svg.appendChild(outerCircle);
const circle=document.createElements(svg.namespaceURI,'circle');
circle.setAttributeNS(null,'cx',offsetX.toString());
circle.setAttributeNS(null,'cy',offsetY.toString());
圆.setAttributeNS(null,'r','7');
circle.setAttributeNS(null,'fill','red');
circle.setAttributeNS(null,'stroke','red');
circle.setAttributeNS(null,'笔划宽度','1');
svg.appendChild(圆);
const number=document.createElements(svg.namespaceURI,'text');
number.setAttributeNS(null,'x',offsetX.toString());
number.setAttributeNS(null,'y',offsetY.toString());
setAttributeNS(null,'text-anchor','middle');
setAttributeNS(null,'stroke','white');
setAttributeNS(空,'笔划宽度','1px');
number.textContent='1';
svg.appendChild(数字)
svg{
边框:1px实心#000000;
背景:黑色;
}

调整文本位置以获得您想要的内容:

number.textContent = '1';
number.setAttributeNS(null, 'fill', 'white');
number.setAttributeNS(null, 'y', offsetY + 5); // 5 in this context, may vary depending on the circle radius
以下是正在运行的示例:


点击事件
svg{
边框:1px实心#000000;
背景:黑色;
}
const svg=document.querySelector('#svg');
const svgNS=svg.namespaceURI;
const rect=document.createElements(svgNS,'rect');
常数offsetX=20;
常数偏移=20;
const outerCircle=document.createElements(svg.namespaceURI,'circle');
setAttributeNS(null,'cx',offsetX.toString());
setAttributeNS(null,'cy',offsetY.toString());
setAttributeNS(null,'r','9');
setAttributeNS(null,'fill','white');
setAttributeNS(null,'stroke','white');
setAttributeNS(null,'笔划宽度','1');
svg.appendChild(outerCircle);
const circle=document.createElements(svg.namespaceURI,'circle');
circle.setAttributeNS(null,'cx',offsetX.toString());
circle.setAttributeNS(null,'cy',offsetY.toString());
圆.setAttributeNS(null,'r','7');
circle.setAttributeNS(null,'fill','red');
circle.setAttributeNS(null,'stroke','red');
circle.setAttribute