javascript - <svg> <defs> <use xlink :href . .. 通过 javascript 的脚本使用失败

标签 javascript html svg

我尝试使用 JavaScript 在 html 中动态创建 svg 路径元素。
我想在 <defs> 中设置路径以后可以在 <use> 中重复使用的元素xlink:href 元素。
创建后(在示例中按下按钮)屏幕的下半部分保持空白。
相同的 html,当静态放置时工作正常。 (按钮上方)
在 Chrome/Firefox 中检查显示,在动态操作的 dom 中,#shadow-root(节点?)在动态创建的部分中是空的,而它在静态部分中持有路径的副本。

有没有办法触发我错过的手动刷新?
这通常是禁止的方式吗?
我的代码有错误吗?

<!DOCTYPE html>
<html>
    <script>
        function test() {
            component = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
            component.classList.add("component");
            component.style.left = '0px';
            component.style.top = '0px';
            component.style.width = '800px';
            component.style.height = '400px';
            component.style.position = "absolute";
            document.getElementById("theConnections").appendChild(component);           

            defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
            component.appendChild(defs);

            pathdef = document.createElementNS('http://www.w3.org/2000/svg', 'path');
            pathdef.id = "conn1";
            pathdef.setAttribute("d", "M264 133 L396 132");
            defs.appendChild(pathdef);

            path2 = document.createElementNS('http://www.w3.org/2000/svg', 'use');
            path2.setAttribute("xlink:href", "#conn1");
            path2.setAttribute("stroke", "black");
            path2.setAttribute("stroke-width", "9");
            component.appendChild(path2);

            path = document.createElementNS('http://www.w3.org/2000/svg', 'use');
            path.setAttribute("xlink:href", "#conn1");
            path.setAttribute("stroke", "white");
            path.setAttribute("stroke-width", "7");
            component.appendChild(path);

        }
    </script>
    <style>
        .dooferKasten {
            background-color: rgb(82, 69, 50);
        }
    </style>
    <body>
        <div style="width:800px; height:400px; position: relative" class="dooferKasten">
            <svg class="component" style="left: 0px; top: 0px; width: 800px; height: 400px; position: absolute;">
                <defs><path id="conn0" d="M264 133 L396 132"></path></defs>
                <use xlink:href="#conn0" stroke="black" stroke-width="9"></use>
                <use xlink:href="#conn0" stroke="white" stroke-width="7"></use>
            </svg>
        </div>
        <button onclick="test()">test</button>
        <div style="width:800px; height:400px; position: relative" id="theConnections" class="dooferKasten">
        </div>      
    </body>
</html>

最佳答案

感谢罗伯特·朗森。这指出了正确的方向。 我读了一篇文章,实际上 xlink:href 属性属于不同的命名空间。所以我不得不使用'http://www.w3.org/1999/xlink ' 而不是 'http://www.w3.org/2000/svg '. 因此,按照一般建议始终在我的 svg 命名空间内使用 setAttributeNS ...

并为非前缀属性使用 null...

并使用正确的命名空间,其中该属性属于一个完全不同的命名空间,我的代码如下所示:

function test() {
    component = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    component.classList.add("component");
    component.style.left = '0px';
    component.style.top = '0px';
    component.style.width = '800px';
    component.style.height = '400px';
    component.style.position = "absolute";
    document.getElementById("theConnections").appendChild(component);           

    defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
    component.appendChild(defs);

    pathdef = document.createElementNS('http://www.w3.org/2000/svg', 'path');
    pathdef.id = "conn1";
    pathdef.setAttributeNS(null, "d", "M264 133 L396 132");
    defs.appendChild(pathdef);

    path2 = document.createElementNS('http://www.w3.org/2000/svg', 'use');
    path2.setAttributeNS('http://www.w3.org/1999/xlink', "xlink:href", "#conn1");
    path2.setAttributeNS(null, "stroke", "black");
    path2.setAttributeNS(null, "stroke-width", "9");
    component.appendChild(path2);

    path = document.createElementNS('http://www.w3.org/2000/svg', 'use');
    path.setAttributeNS('http://www.w3.org/1999/xlink', "xlink:href", "#conn1");
    path.setAttributeNS(null, "stroke", "white");
    path.setAttributeNS(null, "stroke-width", "7");
    component.appendChild(path);

}

关于javascript - <svg> <defs> <use xlink :href . .. 通过 javascript 的脚本使用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55047998/

相关文章:

javascript - HTML 选择值传递到 Javascript var(然后用于获取 JSON)

javascript - 为什么 webpack 需要一个空的扩展

javascript - 如何翻译svg的路径

css - 向网站添加多种 Icomoon 字体

javascript - element.children 的 console.log 显示长度为 0,但稍后展开时有 3 个条目

javascript - 如何获取 jQuery UI 选项卡元素面板并设置其中一些的可见性?

javascript - 使用 Java 脚本时无法设置未定义的属性 'display'

Html CSS 表格行问题

javascript 或 php 函数在文本的开头和结尾添加一些内容

javascript - 将外部 CSS 作为内联样式应用于 HTML 节点(vanilla Javascript/Coffeescript)