如何检查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
对浏览器的支持较少.记住这一点.