return浏览器
var i = function(){函数
return对象
}事件
console.log(i())//undefined回调函数
return的主要做用是阻止函数继续执行,直接返回undefinedio
return falseconsole
<a class="baidu" href="http://www.baidu.com">百度</a>event
$('.baidu').on('click',function(e){function
console.log(1)class
return false
})//1
并未跳转页面,当每次调用return false时,实际作了3件事情
1.event.preventDefault();
2.event.stopPropagation();
3.中止回调函数执行并当即返回
e.preventDefault
$('.baidu').on('click',function(e){
console.log(1)
e.preventDefault()
})//1
e.preventDefault()方法用来阻止浏览器继续执行默认行为,这里阻止了页面的跳转
e.stopPropagation
<div class="btn"><a class="baidu" href="http://www.baidu.com">百度</a></div>
$('.btn').on('click', function () {
console.log(520)
})
$('.btn .baidu').on('click', function (e) {
console.log(1)
e.preventDefault()
e.stopPropagation()
})
输出结果为1
e.stopPropagation阻止事件冒泡
e.stopImmediatePropagation
$('.btn .baidu').on('click',function(e){
console.log(1)
e.preventDefault()
})
$('.btn .baidu').on('click',function(e){
console.log(2)
e.preventDefault()
e.stopImmediatePropagation()
})
$('.btn .baidu').on('click',function(e){
e.preventDefault()
console.log(3)
})
$('.btn').on('click',function(e){
e.preventDefault()
console.log(4)
})
点击输出结果为1,2
e.stopImmediatePropagation()会中止一个事件继续执行,即便当前的对象上还绑定了其余处理函数,全部绑定在一个对象上的事件会按照绑定顺序执行
综上所述
return阻止函数继续执行,返回undefined
return false有三个做用,阻止浏览器默认行为,阻止事件冒泡,中止回调函数执行并当即返回
event.preventDefault阻止浏览器默认行为
event.stopPropagation阻止事件冒泡
event.stopImmediatePropagation中止一个事件继续执行,即便当前的对象上还绑定了其余处理函数,全部绑定在一个对象上的事件会按照绑定顺序执行