javascript - 在 JavaScript 中动态创建 SVG 链接

标签 javascript svg xlink

我正在从 JavaScript 动态创建 SVG 元素。它适用于像矩形这样的可视对象,但我在生成有效的 xlink 时遇到了问题。在下面的示例中,第一个矩形(静态定义的)在点击时可以正常工作,但其他两个(用 JavaScript 创建)忽略点击......即使在 Chrome 中检查元素似乎显示相同的结构。

我见过很多类似的问题,但没有一个能准确解决这个问题。我找到的最接近的是 [ adding image namespace in svg through JS still doesn't show me the picture ] 但这不起作用(如下所述)。我的目标是完全在 JavaScript 中完成此操作,而不依赖于 JQuery 或其他库。


<!-- Static - this rectangle draws and responds to click -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svgTag">
    <a xlink:href="page2.html" id="buttonTemplate">
        <rect x="20" y="20" width="200" height="50" fill="red" class="linkRect"/>
    </a>
</svg>

<script>
    var svgElement = document.getElementById ("svgTag");

    // Dynamic attempt #1 - draws but doesn't respond to clicks
    var link = document.createElementNS("http://www.w3.org/2000/svg", "a");  // using http://www.w3.org/1999/xlink for NS prevents drawing
    link.setAttribute ("xlink:href", "page2.html");  // no improvement with setAttributeNS
    svgElement.appendChild(link);

    var box = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    box.setAttribute("x", 30); 
    box.setAttribute("y", 30);
    box.setAttribute("width", 200);
    box.setAttribute("height", 50);
    box.setAttribute("fill", "blue");
    link.appendChild(box);

    // Dynamic attempt #2 (also draws & doesn't respond) - per https://stackoverflow.com/questions/6893391
    box = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    box.setAttribute("x", 40); 
    box.setAttribute("y", 40);
    box.setAttribute("width", 200);
    box.setAttribute("height", 50);
    box.setAttribute("fill", "green");
    box.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', "page2.html");
    svgElement.appendChild(box);

最佳答案

只有一个<a>可以是链接,因此将 xlink:href 属性添加到 <rect>元素将无效。

您需要使用 setAttributeNS,您说它不起作用,但它对我有用,所以可能还有其他问题。

这个例子对我有用:

var svgElement = document.getElementById ("svgTag");

var link = document.createElementNS("http://www.w3.org/2000/svg", "a");
link.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', "page2.html");
svgElement.appendChild(link);

var box = document.createElementNS("http://www.w3.org/2000/svg", "rect");
box.setAttribute("x", 30); 
box.setAttribute("y", 30);
box.setAttribute("width", 200);
box.setAttribute("height", 50);
box.setAttribute("fill", "blue");
link.appendChild(box);
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svgTag">
</svg>

关于javascript - 在 JavaScript 中动态创建 SVG 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19132443/

相关文章:

javascript - 禁用向上和向下箭头

javascript - 如何在 JavaScript 中对字符串进行哈希处理?

javascript - Chrome 中的 SVG 动态渲染

javascript - SVG 和 d3 : Draw primitive rectangle in data coordinates?

svg - 如何从SVG链接?

javascript - 用 enzyme 和 connect() 进行开 Jest 测试,给出 `import connectAdvanced from ' ../components/connectAdvanced';`

javascript - 我可以将类/ID 附加到使用 JavaScript 以编程方式插入的链接吗?

javascript - 在 x 时间后一个接一个地运行 JavaScript 函数

svg - 内联 svg 不显示在 xhtml 中

php - 如何使用我的 php 文件将事件 URL 附加到我的输出 (XML) 文件