jQuery事件机制

这篇博客介绍了JavaScript中使用bind()和on()方法添加事件处理程序,并展示了事件对象的使用,如event.keyCode和event.preventDefault()。同时,文章还涵盖了each()方法在遍历jQuery对象和DOMList中的应用,以及$.each()和jQuery.each()在遍历数组和对象时的用法。
摘要由CSDN通过智能技术生成

二、注册事件

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)

    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值