当我将 id 设置为 svg 元素时
var circle = paper.circle(x, y, r);
circle.node.id = 'circle-id';
一切正常,当使用调试器浏览文档时,我可以看到这样的预期结果:
<circle cx="320" cy="240" r="4" fill="none" stroke="#000" id="circle-id" />
然后我可以通过 document.getElementById
方法或通过 jQuery 按 id 获取此元素。
但是添加一些其他属性失败了。如果我尝试添加属性 custom
:
circle.node.custom = 'custom-attr';
我看不出有什么影响。
我们可以使用 Raphael 向 SVG 元素添加什么样的属性以及如何添加任意属性?
最佳答案
node
是 DOM 元素,id
是元素的标准属性,用于快速读取/写入其值。使用 setAttribute
方法设置非标准属性。
circle.node.setAttribute('custom', 'custom-attr');
关于javascript - 在 svg 元素上设置自定义属性 (Raphael),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14896395/