二、注册事件
bind() 、on() 方法向被选元素添加一个或多个事件处理程序,以及当事件发生时运行的函数。
// bind() 、on('事件类型',事件处理程序)
/*
bind() 、on({
事件1:function(){
}
事件2:function(){
}
……
})
*/
二、事件对象
$('#user').on('keydown', function () {
console.log(event)
console.log(event.keyCode)
event.preventDefault()
if (event.keyCode == 13) {
window.location.href = 'https://www.baidu.com'
}
})
三、each()方法:遍历jQuery对象、DOMList
$('ul>li').each(function () {
console.log(this)
console.log($(this).index())
})
四、$.each()/jQuery.each():遍历数组或者对象jQuery.each(对象/数组,function(key/index,value/item){})
var arr = [10, 20, 30]
jQuery.each(arr, function (index, item) {
console.log('我是数组的第' + (index + 1) + '个元素' + item)
})
var obj = {
name: '小花',
age: 18,
eat:function(){
console.log('chi');
}
}
$.each(obj, function (key, value) {
console.log(key + ':' + value)
})