Javascript SVG<;使用>;标记现在以编程方式插入时显示

Javascript SVG<;使用>;标记现在以编程方式插入时显示,javascript,html,svg,Javascript,Html,Svg,我一直在关注这个链接,并试图添加一些带有标记的SVG图标。当我“硬编码”SVG并在HTML代码中直接使用标记时,我已经能够使它工作。但是当我尝试从javascript执行完全相同的操作时,代码被正确创建和插入,但是SVG没有显示在屏幕上 以下是我正在使用的代码: 额外信息 addEventListener(“加载”,函数(){ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //添加HTML元素 //>>>>>>>>>>>>>>>>>>>>>>>>>

我一直在关注这个链接,并试图添加一些带有
标记的SVG图标。当我“硬编码”SVG并在HTML代码中直接使用标记时,我已经能够使它工作。但是当我尝试从javascript执行完全相同的操作时,代码被正确创建和插入,但是SVG没有显示在屏幕上

以下是我正在使用的代码:


额外信息
addEventListener(“加载”,函数(){
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//添加HTML元素
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var div=document.createElement(“div”)
div.className=“extraInfo”;
document.getElementById('info').appendChild(div)
var icon=document.createElement(“div”)
icon.className=“信息图标”
div.appendChild(图标)
var iconSVG=document.createElement(“SVG”)
iconSVG.setAttribute(“viewBox”、“0 100 100”)
icon.appendChild(iconSVG)
var svgUse=document.createElement(“使用”)
iconSVG.appendChild(svgUse)
setAttribute(“xlink:href”,“电子邮件”)
})
  • SVG区分大小写(元素名称是SVG而不是SVG)
  • SVG元素需要使用CreateElements在SVG名称空间中创建
  • xlink:href属性需要使用setAttributeNS在xlink命名空间中创建
  • 
    额外信息
    addEventListener(“加载”,函数(){
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //添加HTML元素
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    var div=document.createElement(“div”)
    div.className=“extraInfo”;
    document.getElementById('info').appendChild(div)
    var icon=document.createElement(“div”)
    icon.className=“信息图标”;
    div.appendChild(图标)
    var iconSVG=document.createElements(“http://www.w3.org/2000/svg“,“svg”)
    setAttribute(“viewBox”、“0 100 100”);
    icon.appendChild(iconSVG);
    var svgUse=document.createElements(“http://www.w3.org/2000/svg“,”使用“);
    iconSVG.appendChild(svgUse);
    "的"http://www.w3.org/1999/xlink“,”href“,”电子邮件“;
    })
    
  • SVG区分大小写(元素名称是SVG而不是SVG)
  • SVG元素需要使用CreateElements在SVG名称空间中创建
  • xlink:href属性需要使用setAttributeNS在xlink命名空间中创建
  • 
    额外信息
    addEventListener(“加载”,函数(){
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //添加HTML元素
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    var div=document.createElement(“div”)
    div.className=“extraInfo”;
    document.getElementById('info').appendChild(div)
    var icon=document.createElement(“div”)
    icon.className=“信息图标”;
    div.appendChild(图标)
    var iconSVG=document.createElements(“http://www.w3.org/2000/svg“,“svg”)
    setAttribute(“viewBox”、“0 100 100”);
    icon.appendChild(iconSVG);
    var svgUse=document.createElements(“http://www.w3.org/2000/svg“,”使用“);
    iconSVG.appendChild(svgUse);
    "的"http://www.w3.org/1999/xlink“,”href“,”电子邮件“;
    })
    
    像这样->像这样->很好!非常感谢美好的非常感谢