javascript - 使用 jQuery 更改 SVG 元素的 "xlink:href"属性

标签 javascript jquery html svg

我正在尝试通过单击事件更改“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>

我在这里错过了什么?谢谢!

最佳答案

我今天遇到了同样的问题,在四处搜索之后,我找到了两个有效的解决方案。正如@Dr.Molle 和@prodigitalson 在这个问题的评论中所建议的,我能够使用:_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 setter 执行以下操作:

If the href attribute is not present, the SVGAnimatedString object is defined to additionally reflect the deprecated xlink:href attribute if the deprecated attribute is present. Then it sets that deprecated attribute to the specified value.

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

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/

希望对您有所帮助!

关于javascript - 使用 jQuery 更改 SVG 元素的 "xlink:href"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900472/

相关文章:

Javascript/jQuery 提交表单验证

javascript - 调整 textarea 和 div 的大小——但调整到与 div 调整大小相同的比例

javascript - Jquery parent 不工作

jquery - 即使你刚刚使用了 .remove() 函数,如何在 jquery 中添加一个 div

html - CSS 文本输入字段没有对齐左上角?

javascript - 如何通过 iframe 按钮提交填充父页面内的文本输入?

php - JS 表单元素的问题

javascript - JQuery .click() 事件问题

jquery - 显示以前隐藏的 Bootstrap 警报

html - 如何控制图像高度以匹配 html 电子邮件中的相邻文本