命名函数
可多次调用。
函数声明:
function func(args) {//执行语句};
例:
<script>
$(document).ready(function() {
//创建一个点击事件
$('img').clikc(show);
//show()函数声明
function show() {
var info = '<div><h3></h3></div>';
$(this).after(info);
//显示一次后删除事件
$(this).unbind('click');
}
});
</script>
<body>
<img src='images/1.png' alt='logo'>
</body>
函数表达式:
var func = function(args) {//执行语句};
例:
<script>
$(document).ready(function() {
//创建函数表达式:show
var show = function() {
var info = '<div><h3></h3></div>';
$(this).after(info);
//显示一次后删除事件
$(this).unbind('click');
}
$('img').clikc(show);
});
</script>
<body>
<img src='images/1.png' alt='logo'>
</body>
匿名函数
以参数,回调或闭包形式出现,不可重复调用。
$('button').click(function(args) {//执行语句});
例:
<script>
$(document).ready(function() {
$('img').clikc(function() {
var info = '<div><h3></h3></div>';
$(this).after(info);
//显示一次后删除事件
$(this).unbind('click');
});
});
</script>
<body>
<img src='images/1.png' alt='logo'>
</body>