如何在 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
对浏览器的支持较少。请记住这一点。:)