什么是jQuery for Document.createElementNS()? [英] What is jQuery for Document.createElementNS()?
问题描述
Document.createElementNS()的jQuery是什么?
What is 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);
}
该代码是 Emle-电子数学实验室设备 JavaScript文件 emle_lab.js .
The code is a simplified fragment from Emle - Electronic Mathematics Laboratory Equipment JavaScript file emle_lab.js.
查找功能luf()
将完整引用映射到XPath字符串和createElementNS()
中命名空间的简称.找到现有的svg:svg
,将其删除并替换为新的矩形.
The Look-Up-Function, luf()
, maps a complete reference to a shorten name for the namespace in the XPath string and createElementNS()
. The existing svg:svg
is located, removed and replaced by a new rectangle.
推荐答案
Document.createElementNS()的jQuery是什么?
What is jQuery for Document.createElementNS()?
那将是:
$(document.createElementNS('namespace', 'tag'))
因此,在OP的情况下:
So in the OP's case:
rect = $(document.createElementNS(lu('svg'), 'rect'))
.addClass('emleGraphicOutline')
.attr({
x: 35,
y: 25,
width: 200,
height: 50
});
但是,当然,使用jQuery并不能真正获得多少收益.在任何情况下,您都可以使用以下方式始终将DOM节点包装在jQuery中:用香草JS创建rect
后$(rect)
.
But not much is really gained by using jQuery for that, of course. In any case, one can always wrap DOM nodes in jQuery with e.g. $(rect)
after creating rect
with vanilla JS.
Note that jQuery has other issues with SVG, such as breaking viewBox
due to lowercasing attributes, so plain JS must still be used for some attributes.
这篇关于什么是jQuery for Document.createElementNS()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!