gpt4 book ai didi

javascript - 在 SVG 中操作 的类

转载 作者:行者123 更新时间:2023-11-28 13:33:47 25 4
gpt4 key购买 nike

我在 SVG 中有一个 JavaScript(纯、简单的 JS,没有 jQuery),我在其中获取了 <path>通过 document.getElementById()。现在我尝试向其中添加一个“active”类,但我被卡住了。

这是到目前为止我的(简化的)代码:

var element = document.getElementById(myId);
//A console.log(element) returns the <path>-Object, so I guess this is working.

if (element) {
//Add a class
}

我可以获得 -Object 的当前类:

console.log(element.className.baseVal);

但我无法为此添加“事件”...我已经尝试了几种方法,例如

element.attr('class', className + ' active');

.. 返回

Uncaught TypeError: Object #<SVGPathElement> has no method 'attr'

有没有一种简单的方法可以在空白 JavaScript 中做到这一点?我在这里遗漏了什么吗?

最佳答案

这样做:

element.setAttribute('class', className + ' active');

你试图用 jquery 的语法来做到这一点

关于javascript - 在 SVG 中操作 <path> 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com