加载中...

jQuery.isFunction()


概述    jQuery.isFunction( obj )

返回值:Boolean

描述:确定参数是否为一个Javascript 函数。

  • V : 1.2jQuery.isFunction( obj )

    • obj
      类型: PlainObject
      将要被检查的对象。用于判断该对象是否为函数。

注意:jQuery 1.3以后,例如在 Internet Explorer 中,浏览器提供的函数比如alert()还有 DOM 元素的方法比如 getAttribute() 将不认为是函数。

示例

实例

测试一些参数是否为函数的示例。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div { color:blue; margin:2px; font-size:14px; }
  6. span { color:red; }
  7. </style>
  8. <script src="http://code.jquery.com/jquery-latest.js"></script>
  9. </head>
  10. <body>
  11. <div>jQuery.isFunction(objs[0]) = <span></span></div>
  12. <div>jQuery.isFunction(objs[1]) = <span></span></div>
  13. <div>jQuery.isFunction(objs[2]) = <span></span></div>
  14. <div>jQuery.isFunction(objs[3]) = <span></span></div>
  15. <div>jQuery.isFunction(objs[4]) = <span></span></div>
  16. <script>
  17. function stub() {
  18. }
  19. var objs = [
  20. function () {},
  21. { x:15, y:20 },
  22. null,
  23. stub,
  24. "function"
  25. ];
  26. jQuery.each(objs, function (i) {
  27. var isFunc = jQuery.isFunction(objs[i]);
  28. $("span").eq(i).text(isFunc);
  29. });
  30. </script>
  31. </body>
  32. </html>

Demo:

实例

判断传入的参数是否为函数。

  1. $.isFunction(function(){});

Result:

  1. true


还没有评论.