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

标签 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浏览器支持较少。记住这一点。 :)

关于javascript - jQuery:检查 SVG 元素的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478080/

相关文章:

javascript - Angular,循环遍历 DOM 上已经创建的组件

c# - 将 C# 字符串编码为 Javascript 字符串的注意事项

javascript - $(window).load(function()-它如何与 FF 一起工作

javascript - 调整浏览器大小时,SVG 顶部有空白

svg - 我怎样才能知道 svg 二次贝塞尔曲线的峰值点坐标?

javascript - 我的 fullcalendar 插件中的弃用警告

javascript - 如何从 url 中删除参数?

html - 在 HTML 代码中嵌入 SVG 文件时,如何在不同的浏览器上产生相同的结果?

javascript - 如何删除某个元素后的第一个 <br>

javascript - 仅当经过滚动点时 Bootstrap 附加