概述 jQuery.isFunction( obj )
返回值:Boolean
描述:确定参数是否为一个Javascript 函数。
注意:jQuery 1.3以后,例如在 Internet Explorer 中,浏览器提供的函数比如alert()
还有 DOM 元素的方法比如 getAttribute()
将不认为是函数。
示例
测试一些参数是否为函数的示例。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- div { color:blue; margin:2px; font-size:14px; }
- span { color:red; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
-
- <div>jQuery.isFunction(objs[0]) = <span></span></div>
-
- <div>jQuery.isFunction(objs[1]) = <span></span></div>
- <div>jQuery.isFunction(objs[2]) = <span></span></div>
- <div>jQuery.isFunction(objs[3]) = <span></span></div>
-
- <div>jQuery.isFunction(objs[4]) = <span></span></div>
-
- <script>
- function stub() {
- }
- var objs = [
- function () {},
- { x:15, y:20 },
- null,
- stub,
- "function"
- ];
-
- jQuery.each(objs, function (i) {
- var isFunc = jQuery.isFunction(objs[i]);
- $("span").eq(i).text(isFunc);
- });
- </script>
-
- </body>
- </html>
Demo:
判断传入的参数是否为函数。
- $.isFunction(function(){});
- true