繁体   English   中英

jQuery的textpath svg元素有问题

[英]jQuery having issues with textpath svg elements

我在我的项目中使用SVG ,并希望在单击时在文本路径元素上使用jQuery切换toggleClass

所以我在想:

$("text#names > textpath").click(function() {
    $(this).toggleClass("newClass");
});

在HTML来源上:

<text id="names" class="clickthrough">
    <textpath class="zoom1" href="#text1" font-size="12.1"></textpath>
    <textpath ... </textpath>
</text>

但什么也没发生。 如果我执行$("text#names")我将获得类clickthrough 这样就可以了,只是jQuery可能不知道textpath。 因此,我找到了http://keith-wood.name/svgref.html,但是在使用它之前,我想确定我的情况是否确实需要它。

jQuery的.class不适用于SVG元素,您必须改为使用attr

$("text#names > textpath").click(function() {
  $(this).attr("class", function(_, val){
    return (val.indexOf("newClass")+1) ? val.replace(" newClass","") : val+" newClass";
  });
});

查看jsFiddle演示

暂无
暂无

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

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