ITeye博客

jquery custom event

selfcontroller · 2013-11-26 16:53 · javascrit · Web前端 · Markdown

先说说jquery关于事件的处理方式,在jquery中,函数要想被触发,需要至少四个因素: 1、事件对象 2、事件执行函数 3、将事件执行函数和事件对象进行绑定到某个具体的监听对象上 4、触发该事件

举例:


    //首先定义一个事件处理函数:
    function cust(){
      console.log(' hello every I am the custom function')
    }

    //其次,自定义一个事件
    var e = $.Event('introduce',{name:'intro',data='100'})//利用jquery的Event构造器创建了一个

    //将事件执行函数和事件对象进行绑定到某个具体的监听对象上
    $('body').bind('introduce',cust)

    //触发该事件
    $('body').trigger('introduce')

我们经常用到自定义事件是在写jquery plugin的时候,插件允许用户定义callback,然后我们在插件中根据插件的几个不同的状态来触发用户自定义的callback,我们也可以这样用:


   function cust(e){
    console.log(e.name)
    console.log("====hello I am cust function ===");
   }

   $('a').bind('cust1',cust)

   $('a').trigger({type:'cust1', test:true,name:'gao'});


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。