$(document).ready()
与window.onload
的比较$(document).ready()
方法注册的事件处理程序,在DOM
彻底就绪时就能够被调用,可是,这并不意味着这些元素关联的文件都已经下载完毕,若是脚本中要获取图片的长度与宽度等属性,请不要用此方法。html
window.onload
方法是在网页中全部的元素(包括元素的全部关联文件)彻底加载到浏览器后才执行,即JS
此时才能够访问网页中的任何元素。jquery
$(document).ready()
方法浏览器
function one() { alert('one'); } function two() { alert('two'); } $(document).ready(function() { one(); }); $(document).ready(function() { two(); });
运行代码后,会先弹出字符串one
对话框,而后再弹出字符串two
对话框。app
window.onload
ide
// 省略one与two函数的声明 window.onload = one; window.onload = two;
运行代码后,只弹出字符串two
对话框。由于load
事件一次只能保存对一个函数的引用,它会自动用后面的函数覆盖前面的函数。所以不能在如今的行为上添加新的行为。
能够用下面的方法:函数
window.onload = function () { one(); two(); }
虽然这样的能解决某些问题,但仍是不能知足某些需求。例若有多个js
文件,每一个文件都要用到window.onload
方法,这种状况下用上面的方法就会很是麻烦,而$(document).ready()
就能够处理这些状况。this
bind
bind(type [, data], fn)
spa
type
:事件类型,包括click
、mousedown
等。code
data
:做为event.data
属性传递给事件对象的额外数据对象,可选。htm
fn
:用来绑定的处理函数。
$(function() { $('#panel h5.header').bind('mouseover', function(event) { $(this).next().show(); }).bind('mouseout', function(event) { $(this).next().hide(); }); });
hover()
方法
hover(enterFn, leaveFn)
hover()
方法用于模拟光标悬停事件。当光标移动到元素上时,会触发指定的第一个函数(enterFn
),当光标移出这个元素时,会触发指定的第二个函数(leaveFn
)。hover()
方法替代的是bind('mouseenter')
与bind('mouseleave')
。
$(function() { $('.#panel h5.header').hover(function() { $(this).next().show(); }, function() { $(this).next().hide(); }); });
<body> <div class = "content"> <span></span> </div> </body>
// 为span绑定事件 $('span').bind('click', function(event) { alert('Span is clicked.'); }); // 为div绑定事件 $('.content').bind('click', function(event) { alert('Div is clicked.'); }); // 为body绑定事件 $('body').bind('click', function(event) { alert('Body is clicked.'); });
当点击<span>
元素时,<span>
、<div>
、<body>
元素的click
事件依次被触发。
事件会按照DOM
的层次结构像水泡同样不断向上直至顶端。
中止事件冒泡
事件冒泡会引发预料以外的效果。可用stopPropagation()
方法来中止冒泡。
$('span').bind('click', function(event) { event.stopPropagation(); alert('Span is clicked.'); });
此时点击<span>
元素时,只会执行<span>
的click
事件。
阻止默认行为
可用preventDefault()
方法来阻止元素的默认行为。
举例,在项目中,常常须要验证表单,在单击“提交”按钮时,验证表单内容,例如某元素是不是必填字段,某元素长度是否够6位等,当表单不符合提交条件时,要阻止表单的提交(默认行为)。
$(function() { $('#sub').bind('click', function(event) { var username = $('#username').val(); if (username === '') { $('#msg').html('<p>文框的值不能为空</p>'); event.preventDefault(); } }); });
若是想同时对事件对象中止冒泡和默认行为,能够在事件处理函数return false
。这是对在事件对象上同时调用stopPropagation()
方法和event.preventDefault()
。
事件捕获
事件捕获和事件冒泡是恰好相反的两个过程,事件捕获是从最顶端往下开始触发。
并不是全部主流浏览器都支持事件捕获,而且这个缺陷没法经过js
来修复。
`unbind([type], [data]);
第一个参数是事件类型,第二个参数是将要移除的函数:
若是没有参数,则删除全部绑定的事件。
若是提供了事件类型做为参数,则只删除该类型的绑定事件。
若是把绑定时传递的函数做为第二个参数,则只有这个特定的事件处理函数会被删除。
one()
方法
one(type, [data], fn)
one()
方法的结构与bind()
方法相似,使方法也与bind()
方法相同。使用one()
方法为元素绑定事件后,只在第一次触发事件时,处理函数才执行,以后的触发毫无做用。
trigger(type, [data])
第一个参数是要触发的事件类型
第二个参数是要传递给事件处理函数的附加数据
使用trigger()
方法
$('#btn').trigger('click'); // or $('#btn).click();
$('#btn').bind('myClick', function () { // do sth }); $('#btn').trigger('myClick');
// 获取数据 $('#btn').bind('myClick', function(event, msg1, msg2) { $('#test').append('<p>' + msg1 + msg2 + '</p>'); }); // 传递两个数据 $('#btn').trigger('myClick', ['个人自定义', '事件']);
trigger()
方法触发事件后,会执行浏览器默认操做。
若是只想触发绑定的事件,而不想执行浏览器的默认操做,可使用triggerHandler()
方法。
$('input').triggerHandler('focus');
$(function() { $('div').bind('mouseover mouseout', function(event) { $(this).toggleClass('over'); }); });
$(function() { $('div').bind('click.plugin', function(event) { $('body').append('<p>click event</p>'); }); $('div').bind('mouseover.plugin', function (event) { $('body').append('<p>mouseover event</p>'); }) $('div').bind('dbclick', function(event) { $('body').append('<p>dbclick event</p>'); }); $('button').bind('click', function(event) { $('div').unbind('.plugin'); }); });
点击<button>
元素后,.plugin
的命名空间被删除,而dbclick
事件依然存在。
$(function() { $('div').bind('click', function(event) { $('body').append('<p>click event</p>'); }); $('div').bind('click.plugin', function(event) { $('body').append('<p>click.plugin event</p>'); }); $('button').bind('click', function(event) { $('div').trigger('click!'); // 注意click后面的叹号 }); });
trigger('click!')
中叹号
的做用是只触发click
事件,不触发click.plugin
事件。