用于判断函数是否可用于对象的js代码

时间:2012-12-03 18:13:05

标签: javascript

以下javascript行获取Uncaught TypeError:对象#Text在chrome和safari中没有方法'getAttribute',但在IE中没有。

this.Element.getAttribute("whatever")

我知道这个。元素是主要问题,但是想要调试代码的其他部分的临时修复。 如何在没有出现javascript错误的情况下测试某项功能是否可用?

2 个答案:

答案 0 :(得分:3)

if (this.Element.getAttribute)
{
// exists
}
else
{
// does not
}

答案 1 :(得分:2)

if (this && this.Element && typeof this.Element.getAttribute == "function") {
    // ...
}