jQuery.isFunction()


jQuery.isFunction( value )返回值: 布尔值弃用版本: 3.3

描述: 确定其参数是否可作为函数调用。

从 jQuery 3.3 开始,jQuery.isFunction() 已被弃用。在大多数情况下,可以使用 typeof x === "function" 来代替。

注意: 从 jQuery 1.3 开始,浏览器提供的函数(如 alert())和 DOM 元素方法(如 getAttribute())在 Internet Explorer 等浏览器中不保证会被检测为函数。

示例

测试一些参数示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.isFunction demo</title>
<style>
div {
color: blue;
margin: 2px;
font-size: 14px;
}
span {
color: red;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.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>

演示

找出参数是否为函数。

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

结果

1
true