动态创建的 SVG 在 Firefox 中不起作用,但在 Chrome 中有效
Dynamically created SVG not working in Firefox, but works in Chrome
我想弄清楚为什么这段代码在 Firefox 中不起作用。它应该创建水平路径,但我在 Firefox 中看不到它们。 Chrome 和 IE 正确显示它们。可能是什么问题?
https://jsfiddle.net/7a6qm371/
<div>
<svg width="100%" height="500" id="svgBottomWall">
<g style="stroke: aqua; fill: none;" id="svgBottomWallGridGroup"></g>
</svg>
$(document).ready(function () {
var svgBottomWall = document.getElementById("svgBottomWall");
var rect = svgBottomWall.getBoundingClientRect();
var svgW = rect.width;
function createHorizontalLine(w, d) {
var nline = document.createElementNS("http://www.w3.org/2000/svg", "path");
nline.setAttribute("d", "M 0 " + d + ", L " + w + " " + d);
nline.setAttribute("stroke-width", 1);
nline.setAttribute("stroke", "#ffffff");
document.getElementById("svgBottomWallGridGroup").appendChild(nline);
}
for (var i = 0; i <= svgW; i = i + 100) {
createHorizontalLine(svgW, i);
}
});
您的 d
路径参数格式不正确。
你有类似的东西
d="M 0 100, L 1000 100"
而应该是
d="M 0,100 L 1000,100"
见https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
修复是
nline.setAttribute("d", "M 0," + d + " L " + w + "," + d);
我想弄清楚为什么这段代码在 Firefox 中不起作用。它应该创建水平路径,但我在 Firefox 中看不到它们。 Chrome 和 IE 正确显示它们。可能是什么问题?
https://jsfiddle.net/7a6qm371/
<div>
<svg width="100%" height="500" id="svgBottomWall">
<g style="stroke: aqua; fill: none;" id="svgBottomWallGridGroup"></g>
</svg>
$(document).ready(function () {
var svgBottomWall = document.getElementById("svgBottomWall");
var rect = svgBottomWall.getBoundingClientRect();
var svgW = rect.width;
function createHorizontalLine(w, d) {
var nline = document.createElementNS("http://www.w3.org/2000/svg", "path");
nline.setAttribute("d", "M 0 " + d + ", L " + w + " " + d);
nline.setAttribute("stroke-width", 1);
nline.setAttribute("stroke", "#ffffff");
document.getElementById("svgBottomWallGridGroup").appendChild(nline);
}
for (var i = 0; i <= svgW; i = i + 100) {
createHorizontalLine(svgW, i);
}
});
您的 d
路径参数格式不正确。
你有类似的东西
d="M 0 100, L 1000 100"
而应该是
d="M 0,100 L 1000,100"
见https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
修复是
nline.setAttribute("d", "M 0," + d + " L " + w + "," + d);