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

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...