[SVG折线仅在修改检查器上的HTML之后才会呈现

问题描述 投票:0回答:1

我的代码将折线添加到SVG,但这些折线未呈现。

但是如果我打开检查器(在Chrome上为F12),请在源代码中找到它们,右键单击它们,将其编辑为HTML,添加一个空格,然后按Enter,它们就会被渲染!

我在这里想念什么?

function drawLine(x1, y1, x2, y2) {
    var line = document.createElement("polyline");
    line.style.cssText = "fill:none;stroke:black;stroke-width:2";

    var linePoints = `${x1},${y1} ${x2},${y2}`;
    line.setAttribute('points', linePoints);

    window.linesContainer.appendChild(line);
}

enter image description here

javascript html polyline
1个回答
0
投票

对于非HTML元素(在本例中为SVG),应使用setAttributeNS

所以:

setAttributeNS

成为

line.setAttribute('points', linePoints);

查看更多:line.setAttributeNS('http://www.w3.org/2000/svg','points', linePoints);

© www.soinside.com 2019 - 2024. All rights reserved.