简体   繁体   English

使用jQuery更改SVG元素的“ xlink:href”属性

[英]Use jQuery to change “xlink:href” attribute of SVG element

I'm trying to change the "xlink:href" attribute with a click event, and so far it is partially working. 我正在尝试通过click事件更改“ xlink:href”属性,到目前为止,它仍在部分起作用。 This is what I'm doing 这就是我在做的

HTML: 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: 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");



 }

 });

What is happening is that I'm able to change the class without any problems, but the "xlink:href" attribute doesn't change, instead, leaves the old one ("#icon-pencil"), and adds a new "href" attribute (href="#icon-floppy-disk"): 发生的事情是,我可以毫无问题地更改类,但是“ 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>

What am I missing here? 我在这里想念什么? Thanks! 谢谢!

I had this same question today and after searching around I found two solutions that worked. 我今天也遇到了同样的问题,在搜索后发现了两个可行的解决方案。 As suggested by @Dr.Molle and @prodigitalson in this question's comments, I was able to use: _HTMLNode_.setAttributeNS() to fix my problem but I am not sure why this solution failed to work for you @cubanGuy. 正如@ Dr.Molle和@prodigitalson在此问题的评论中所建议的那样,我能够使用: _HTMLNode_.setAttributeNS()解决我的问题,但是我不确定为什么该解决方案对您@cubanGuy无效。

After digging through SVG Spec, I learned that xlink:href is deprecated in favor of using a non-namespaced href attribute. 在浏览了SVG Spec之后,我了解到不赞成使用xlink:href ,而赞成使用非命名空间的href属性。 The href attribute (on SVG elements) is represented with a SVGAnimatedString object (used to reflect an animatable string attribute) which has two properties: href属性(在SVG元素上)由SVGAnimatedString对象(用于反映可动画的字符串属性)表示,该对象具有两个属性:

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

This enables us to change the value of the href attribute by setting _HTMLNode_.href.baseVal which also changes the xlink:href attribute because the baseVal setter does the following: 这使我们能够通过设置_HTMLNode_.href.baseVal来更改href属性的值,这也将更改xlink:href属性,因为baseVal设置程序会执行以下操作:

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. 如果href属性不存在,则定义SVGAnimatedString对象以另外反映不赞成使用的xlink:href属性(如果存在不赞成使用的属性)。 Then it sets that deprecated attribute to the specified value. 然后,它将不推荐使用的属性设置为指定的值。

Since the namespaced attribute is deprecated I recommend that _HTMLNode_.href.baseVal be used in favor of _HTMLNode_.setAttributeNS() when supporting modern browsers. 由于不推荐使用命名空间属性, _HTMLNode_.href.baseVal在支持现代浏览器时,建议使用_HTMLNode_.href.baseVal_HTMLNode_.setAttributeNS() If you want to see them in action I created a snippet that demonstrates both methods working below: 如果您想看到它们的实际效果,我创建了一个片段,演示了下面两种方法的工作方式:

 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> 

Here is a working JSFiddle: https://jsfiddle.net/ybtq9e03/ 这是一个可工作的JSFiddle: https ://jsfiddle.net/ybtq9e03/

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM