简体   繁体   English

动态创建的SVG无法在Firefox中运行,但可在Chrome中运行

[英]Dynamically created SVG not working in Firefox, but works in Chrome

I am trying to figure out why this code of is not working in Firefox. 我试图弄清楚为什么这段代码在Firefox中不起作用。 It's supposed to create horizontal paths, but I cannot see them in Firefox. 它应该创建水平路径,但我无法在Firefox中看到它们。 Chrome and IE showing them properly. Chrome和IE正确显示它们。 What could be the issue? 可能是什么问题?

https://jsfiddle.net/7a6qm371/ 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);
}
});

Your d path parameters are incorrectly formatted. 您的d路径参数格式不正确。

You have something like 你有类似的东西

d="M 0 100, L 1000 100"

whereas it should be 应该是

d="M 0,100 L 1000,100"

See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d 请参阅https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d

The fix is 修复是

nline.setAttribute("d", "M 0," + d + " L " + w + "," + d);

JSFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM