微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-jQuery:检查SVG元素的类型

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐