【问题标题】:snap svg add/remove link attributesnap svg 添加/删除链接属性
【发布时间】:2016-01-08 02:12:48
【问题描述】:

我可以像这样使用 ECMAScript 操作 SVG 链接元素的 xlink:href 属性:

var xlinkns = "http://www.w3.org/1999/xlink";

myLink.setAttributeNS(xlinkns, "xlink:href", "#");//add href attribute to the link

myLink.removeAttributeNS(xlinkns, "href");//remove the href attribute from the link

我的问题是:用 javascript 或 snap SVG 做同样事情的正确语法是什么?

【问题讨论】:

  • JavaScript 是 ECMAScript 的一种实现。你做的完全一样。 API 也不是 ECMAScript 的一部分。这是DOM API,它与语言无关。
  • Felix,我知道 javascript 是 ECMAScript 的一种实现,因此我尝试过:myLink.setAttributeNS(xlinkns, "xlink:href", "google.com"); 它没有不行。

标签: javascript svg snap.svg


【解决方案1】:

一闪而过

element.attr("xlink:href", "http://google.com");

Snap 会为你找出命名空间

在 Ecmacript(它是 javascript 的同义词)中

var xlinkns = "http://www.w3.org/1999/xlink";
var myLink = document.getElementById("link");
myLink.setAttributeNS(xlinkns, "href", "http://google.com");
alert(myLink.getAttributeNS(xlinkns, "href"));
<svg><a id="link" xlink:href="http://stackoverflow.com"/></svg>

【讨论】:

  • 谢谢罗伯特,它可以工作...有一件小事仍然困扰着我:如何从 snap 中的链接元素中实际“删除” xlink:href 属性? var xlinkns = "w3.org/1999/xlink"; myLink.removeAttributeNS(xlinkns, "href");换句话说:你能把上面的 ecmascript 语句翻译成
  • 你总是可以突破到普通的 DOM 方法,即 element.node.removeAttributeNS(xlinkNS, "href");
  • 很遗憾,它不起作用@Robert,myLink.removeAttributeNS(xlinkns, "href"); 似乎无法被 snap 识别(我在 ecmascript 标签中使用它)
  • 啊啊,再次感谢您!抱歉,我觉得你打错了,所以我不加.node 部分
猜你喜欢
  • 2011-11-01
  • 2021-08-11
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多