jQuery test for whether an object has a method?

后端 未结 6 1024
一整个雨季
一整个雨季 2020-12-08 11:09

Is it possible to test whether a jQuery object has a particular method? I\'ve been looking, but so far without success. Thanks!

6条回答
  •  执笔经年
    2020-12-08 11:23

    //Simple function that will tell if the function is defined or not
    function is_function(func) {
        return typeof window[func] !== 'undefined' && $.isFunction(window[func]);
    }
    
    //usage
    
    if (is_function("myFunction") {
            alert("myFunction defined");
        } else {
            alert("myFunction not defined");
        }
    

提交回复
热议问题