JavaScript createElement和SVG

pat*_*pat 29 javascript svg capitalization

我想使用Javascript创建内联SVG图形.

但是,似乎createElementNS函数应用了一些规范化并将所有标记转换为小写.这适用于HTML,但不适用于XML/SVG.我使用的NS是http://www.w3.org/2000/svg.

特别是我在创建元素时遇到了问题.因为它将被附加为因此将不起作用.

我做了一些搜索,但还没找到解决方案.

有人知道解决方案吗?

非常感谢!

document.createElementNS("http://www.w3.org/2000/svg","textPath");
Run Code Online (Sandbox Code Playgroud)

结果是

<textpath></textpath>
Run Code Online (Sandbox Code Playgroud)

gum*_*ape 44

我希望,以下示例将帮助您:

function CreateSVG() {
    var xmlns = "http://www.w3.org/2000/svg";
    var boxWidth = 300;
    var boxHeight = 300;

    var svgElem = document.createElementNS(xmlns, "svg");
    svgElem.setAttributeNS(null, "viewBox", "0 0 " + boxWidth + " " + boxHeight);
    svgElem.setAttributeNS(null, "width", boxWidth);
    svgElem.setAttributeNS(null, "height", boxHeight);
    svgElem.style.display = "block";

    var g = document.createElementNS(xmlns, "g");
    svgElem.appendChild(g);
    g.setAttributeNS(null, 'transform', 'matrix(1,0,0,-1,0,300)');

    // draw linear gradient
    var defs = document.createElementNS(xmlns, "defs");
    var grad = document.createElementNS(xmlns, "linearGradient");
    grad.setAttributeNS(null, "id", "gradient");
    grad.setAttributeNS(null, "x1", "0%");
    grad.setAttributeNS(null, "x2", "0%");
    grad.setAttributeNS(null, "y1", "100%");
    grad.setAttributeNS(null, "y2", "0%");
    var stopTop = document.createElementNS(xmlns, "stop");
    stopTop.setAttributeNS(null, "offset", "0%");
    stopTop.setAttributeNS(null, "stop-color", "#ff0000");
    grad.appendChild(stopTop);
    var stopBottom = document.createElementNS(xmlns, "stop");
    stopBottom.setAttributeNS(null, "offset", "100%");
    stopBottom.setAttributeNS(null, "stop-color", "#0000ff");
    grad.appendChild(stopBottom);
    defs.appendChild(grad);
    g.appendChild(defs);

    // draw borders
    var coords = "M 0, 0";
    coords += " l 0, 300";
    coords += " l 300, 0";
    coords += " l 0, -300";
    coords += " l -300, 0";

    var path = document.createElementNS(xmlns, "path");
    path.setAttributeNS(null, 'stroke', "#000000");
    path.setAttributeNS(null, 'stroke-width', 10);
    path.setAttributeNS(null, 'stroke-linejoin', "round");
    path.setAttributeNS(null, 'd', coords);
    path.setAttributeNS(null, 'fill', "url(#gradient)");
    path.setAttributeNS(null, 'opacity', 1.0);
    g.appendChild(path);

    var svgContainer = document.getElementById("svgContainer");
    svgContainer.appendChild(svgElem);
}
Run Code Online (Sandbox Code Playgroud)

  • 这对人们有用吗?我只是得到一个没有任何SVG元素的灰色框.(Firefox和Chrome一样) (2认同)

dli*_*itz 11

首先,使用createElementNS,就像你一样.根据Mozilla文档,createElement(不带NS)会自动缩小HTML文档中的元素名称.

其次,不要相信Google Chrome的"Inspect Element"功能.无论实际的nodeName是什么,它似乎都以小写形式显示每个元素.试试这个:

document.createElementNS("http://www.w3.org/2000/svg", "textPath").nodeName
// Output: "textPath"

document.createElement("textPath").nodeName
// Output: "TEXTPATH"
Run Code Online (Sandbox Code Playgroud)

您的问题可能是一个无关的问题.例如,此代码在Firefox中运行良好,但在Chrome中断(12.0.742.112):

function animateSVG() {
  var svgNS = "http://www.w3.org/2000/svg";
  var textElement = document.getElementById("TextElement");
  var amElement = document.createElementNS(svgNS, "animateMotion");
  console.log(textElement);
  console.log(amElement);
  console.log(amElement.nodeName);
  amElement.setAttribute("path", "M 0 0 L 100 100");
  amElement.setAttribute("dur", "5s");
  amElement.setAttribute("fill", "freeze");
  textElement.appendChild(amElement);
  //amElement.beginElement();
};
Run Code Online (Sandbox Code Playgroud)

我的问题可能与Chrome中animateMotion的处理失败有关(问题13585).

您的问题可能相同,或者可能是另一个问题,但请确保您不会被元素检查器愚弄.

  • 您对chromes inspect element功能的评论保存了我的一天,谢谢 (2认同)