容易出错的jquery扩展

时间:2012-12-12 21:25:43

标签: javascript jquery

任何熟悉模式的人:

  • 最安全地检查现有的jQuery扩展?
  • 如果代码无法执行,如何安全地退出。

喜欢

(function($){
    if (jQuery.fn.pluginName)
    ......
    }
})(jQuery);

10x,BR

1 个答案:

答案 0 :(得分:1)

$ .fn命名空间填充了函数。如果插件存在,您可以使用此

if ($.fn.myPlugin instanceof Function) {
    // the plugin exists
} else {
    // the plugin does not exist
}

如果这还不够,你也可以使用

进行检查
if (typeof $.fn.myPlugin === 'function') {
    // The plugin exists
} else {
    // The plugin does not exist
}
相关问题