javascript 查找 SVG 元素的类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24438085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:48:05  来源:igfitidea点击:

Find class name of SVG element

javascriptjquerysvgclassname

提问by Cute_Ninja

How can I find the class name of SVG attribute?

如何找到 SVG 属性的类名?

If it was div it would have been {reference to DIV}.className

如果它是 div 它将是 {reference to DIV}.className

But, this doesnt work exactly the same way with SVG element

但是,这与 SVG 元素的工作方式并不完全相同

Is there any jquery or javascript method/attribute to get the classes of a SVG element?

是否有任何 jquery 或 javascript 方法/属性来获取 SVG 元素的类?

See below what I have tried:

请参阅下面我尝试过的内容:

enter image description here

在此处输入图片说明

回答by mdlars

Here's a jsfiddle: http://jsfiddle.net/Ujp76/

这是一个 jsfiddle:http: //jsfiddle.net/Ujp76/

<body>
   <svg class="c1"></svg>

   <script>
      window.addEventListener("load", function () {
          var className = document.querySelector("svg").getAttribute("class");
          console.log(className);
      });
   </script>
</body>