11

Document.createElementNS() 的 jQuery 是什么?

function emleGraphicToSvg(aGraphicNode) {
  var lu = function luf(aPrefix){
    switch (aPrefix){
      case 'xhtml': return 'http://www.w3.org/1999/xhtml';
      case 'math':  return 'http://www.w3.org/1998/Math/MathML';
      case 'svg':   return 'http://www.w3.org/2000/svg';
      }
    return '';
    };
  var svg = document.evaluate("svg:svg",
    aGraphicNode, lu, XPathResult.FIRST_ORDERED_NODE_TYPE, null).
    singleNodeValue;
  $(svg).children().remove();
  rect = document.createElementNS(lu('svg'), "rect");
  rect.setAttribute("x", "35");
  rect.setAttribute("y", "25");
  rect.setAttribute("width", "200");
  rect.setAttribute("height", "50");
  rect.setAttribute("class", "emleGraphicOutline");
  svg.appendChild(rect);
  }

该代码是来自Emle - 电子数学实验室设备JavaScript 文件emle_lab.js的简化片段。

Look-Up-Functionluf()将完整引用映射到 XPath 字符串中命名空间的缩短名称和createElementNS(). 现有的svg:svg被定位、移除和替换为一个新的矩形。

4

2 回答 2

5

Document.createElementNS() 的 jQuery 是什么?

那将是:

$(document.createElementNS('namespace', 'tag'))

所以在OP的情况下:

rect = $(document.createElementNS(lu('svg'), 'rect'))
    .addClass('emleGraphicOutline')
    .attr({
        x: 35,
        y: 25,
        width: 200,
        height: 50
    });

但是,当然,为此使用 jQuery 并没有真正获得什么。在任何情况下,总是可以在使用 vanilla JS$(rect)创建之后将 DOM 节点包装在 jQuery 中。rect

请注意,jQuery 与 SVG 有其他问题,例如viewBox由于小写的属性而中断,因此某些属性仍必须使用纯 JS。

于 2017-06-21T00:25:00.987 回答
2

对于 SVG,我将Keith Wood 的 jquery.svg用于一些评估类型的项目。

于 2010-04-03T19:10:14.967 回答