如何在 JavaScript 或 jQuery 中检查 svg 对象的类型?
我想检查标签是否属于 SVGAnimatedString
类型。
当我将对象输出到控制台时,它会输出以下内容:
console.log(this.href);
SVGAnimatedString // is an object and can be expanded
在我的代码中,我尝试检查它是否是 SVG 对象,但检查不起作用。
if (this.href == 'SVGAnimatedString'){ //this check does not work
//it s an svg object
var url = this.href.animVal
}
else{
var url = this.href; //get the href from the <a> element
}
如何正确检查它是否是 SVGAnimatedString
对象?
最佳答案
您不应该使用 ==
来比较类型。您需要使用 instanceof
。你可以这样做:
if (this.href instanceof SVGAnimatedString){ //this check works!!!
//it s an svg object
var url = this.href.animVal
}
else{
var url = this.href; //get the href from the <a> element
}
SVGAnimatedString
浏览器支持较少。记住这一点。 :)
关于javascript - jQuery:检查 SVG 元素的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478080/