我的代码将折线添加到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);
}
对于非HTML元素(在本例中为SVG),应使用setAttributeNS
所以:
setAttributeNS
成为
line.setAttribute('points', linePoints);
查看更多:line.setAttributeNS('http://www.w3.org/2000/svg','points', linePoints);