Home  >  Article  >  Web Front-end  >  How to determine whether a function exists in jquery

How to determine whether a function exists in jquery

藏色散人
藏色散人Original
2021-01-12 09:41:352981browse

Jquery method to determine whether a function exists: first create a code sample file; then pass "if($.isFunction(fun)){eval("testFun('txt')");}}catch( e){$.alert...}" statement can be used to determine whether the function exists.

How to determine whether a function exists in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

Recommended: jQuery video tutorial

jquery determines whether the function exists?

You can use the $.isFunction() function in jquery to determine whether the function exists. The $.isFunction() function is used to determine whether the specified parameter is a function.

Example:

var fun = "testFun";  // 函数的名称
try{
 if($.isFunction(fun)){
     eval("testFun('txt')");   // "txt"是函数参数
 } 
}catch(e){ 
  $.alert(fun +'不是函数!'); 
}

Note the use of "eval()"!!!

The above is the detailed content of How to determine whether a function exists in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn