【问题标题】:Use jQuery to change "xlink:href" attribute of SVG element使用 jQuery 更改 SVG 元素的“xlink:href”属性
【发布时间】:2016-06-07 06:15:13
【问题描述】:

我正在尝试通过单击事件更改“xlink:href”属性,到目前为止它部分工作。这就是我正在做的事情

HTML:

 <a href="#" class="ui-btn ui-corner-all ui-shadow editIcon" data-iconpos="top" data-transition="none" style="text-align:center">
<svg class="icon icon-pencil">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-pencil">   </use>
</svg>
</a>

JS:

 $('a.editIcon').on('click', function () {


 if ($("a.editIcon svg").attr('class') == 'icon icon-pencil') {
     $("a.editIcon svg").attr("class", "icon icon-floppy-disk");
     $("a.editIcon svg use").attr("xlink:href", "#icon-floppy-disk");


 } else {
     myFunctionCall();
     $("a.editIcon svg").attr("class", "icon icon-pencil");
     $("a.editIcon svg use").attr("xlink:href", "#icon-pencil");



 }

 });

发生的事情是我能够毫无问题地更改类,但“xlink:href”属性没有改变,而是留下旧的(“#icon-pencil”),并添加一个新的“href”属性(href="#icon-floppy-disk"):

<svg class="icon icon-floppy-disk">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-pencil" href="#icon-floppy-disk"></use>
</svg>

我在这里缺少什么?谢谢!

【问题讨论】:

  • 尝试在: 之前放置两个反斜杠:\\: 或改用$('[xlink\\:href]')。
  • 除非有插件,否则我认为 jQuery 不会让您设置命名空间属性。您可能需要使用原生 DOM 函数:$("a.editIcon svg use").get(0).setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#icon-pencil');
  • 感谢大家的帮助。实际上,@Dr. Molle 和@prodigitalson 具有相同的效果,它留下了xlink:href="#icon-pencil" href="#icon-floppy-disk。我想我必须找到另一种方法,可能是 replaceWith() 并替换整个东西。

标签: javascript jquery html svg


【解决方案1】:

我今天也有同样的问题,经过四处搜索后,我发现了两个有效的解决方案。正如@Dr.Molle 和@prodigitalson 在这个问题的cmets 中所建议的那样,我能够使用:_HTMLNode_.setAttributeNS() 来解决我的问题,但我不确定为什么这个解决方案对你@cubanGuy 不起作用。

在深入了解 SVG 规范后,我了解到 xlink:href 已被弃用,而应使用非命名空间的 href 属性。 href 属性(在 SVG 元素上)由 SVGAnimatedString 对象 (used to reflect an animatable string attribute) 表示,该对象具有两个属性:

interface SVGAnimatedString {
         attribute DOMString baseVal;
readonly attribute DOMString animVal;
};

这使我们能够通过设置 _HTMLNode_.href.baseVal 来更改 href 属性的值,这也会更改 xlink:href 属性,因为 baseVal 设置器执行以下操作:

如果 href 属性不存在,则定义 SVGAnimatedString 对象以额外反映已弃用的 xlink:href 属性(如果存在已弃用的属性)。然后它将不推荐使用的属性设置为指定的值。

由于不推荐使用命名空间属性,我建议在支持现代浏览器时使用_HTMLNode_.href.baseVal 来支持_HTMLNode_.setAttributeNS()。如果您想查看它们的实际效果,我创建了一个 sn-p,它演示了以下两种方法的工作原理:

var svgElement1 = document.getElementById("editIcon1");
svgElement1.onclick = function () {
	var useElement = this.getElementsByTagName("use")[0];
   if (this.className === 'icon icon-locked') {
       this.className = "icon icon-unlocked";
       useElement.href.baseVal = "#unlocked";
   } else {
       this.className = "icon icon-locked";
       useElement.href.baseVal = "#locked";
   }
 }
 var svgElement2 = document.getElementById("editIcon2");
svgElement2.onclick = function () {
	var useElement = this.getElementsByTagName("use")[0];
   if (this.className === 'icon icon-locked') {
       this.className = "icon icon-unlocked";
       useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#unlocked');
   } else {
       this.className = "icon icon-locked";
       useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#locked');
   }
 }
<svg xmlns="http://www.w3.org/2000/svg" id="svg-icons">
  <symbol viewBox="0 0 24 24" id="unlocked">
    <path d="M18,9h-1V7c0-2.8-2.2-5-5-5S7,4.2,7,7h1.9c0-1.7,1.4-3.1,3.1-3.1s3.1,1.4,3.1,3.1v2H6c-1.1,0-2,0.9-2,2v9c0,1.1,0.9,2,2,2
             h12c1.1,0,2-0.9,2-2v-9C20,9.9,19.1,9,18,9z"></path>
  </symbol>
  <symbol viewBox="0 0 24 24" id="locked">
    <path d="M18,9h-1V7c0-2.8-2.2-5-5-5S7,4.2,7,7v2H6c-1.1,0-2,0.9-2,2v9c0,1.1,0.9,2,2,2h12c1.1,0,2-0.9,2-2v-9C20,9.9,19.1,9,18,9z
             M15.1,9H8.9V7c0-1.7,1.4-3.1,3.1-3.1s3.1,1.4,3.1,3.1V9z"></path>
  </symbol>
</svg>
  
<div>
  <a href="#" id="editIcon1">this is test 1
    <svg class="icon icon-locked">
      <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#unlocked">   </use>
    </svg>
  </a>
</div>
<div>
  <a href="#" id="editIcon2">this is test 2
    <svg class="icon icon-locked">
      <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#unlocked"></use>
    </svg>
  </a>
</div>

这是一个有效的 JSFiddle:https://jsfiddle.net/ybtq9e03/

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 2012-06-19
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2014-11-14
    • 2018-03-29
    相关资源
    最近更新 更多