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
Find class name of SVG element
提问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:
请参阅下面我尝试过的内容:
回答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>