什么是jQuery for Document.createElementNS()?

CW *_* II 11 jquery xml-namespaces

什么是jQuery for Document.createElementNS()?

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);
  }
Run Code Online (Sandbox Code Playgroud)

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

Look-Up-Function luf()将完整引用映射到XPath字符串中的命名空间的缩短名称createElementNS().现有svg:svg的位置,删除并替换为新的矩形.

Jas*_*n C 5

什么是jQuery for Document.createElementNS()?

那将是:

$(document.createElementNS('namespace', 'tag'))
Run Code Online (Sandbox Code Playgroud)

因此,在OP的情况下:

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

但是,当然,使用jQuery并不能真正获得多少收益。无论如何,使用香草JS $(rect)创建之后,总是可以使用jQuery将DOM节点包装起来rect

请注意,jQuery在SVG方面还有其他问题,例如viewBox由于属性改写而造成的中断,因此对于某些属性,仍必须使用普通的JS。


ndi*_*dim 2

对于 SVG,我在一些评估类型项目中使用了Keith Wood 的 jquery.svg

  • svg 只是一个例子。我假设 jquery.svg 不用于处理 svg 以外的名称空间。我正在寻找如何使用 Jquery 处理名称空间,特别是 createElementNS()。 (3认同)