---
title: "jquery custom event"
author: "selfcontroller"
date: "2013-11-26T16:53:44Z"
tags: []
categories: ["Web前端"]
slug: "jquery-custom-event"
draft: false
lastmod: "2013-11-26T16:53:44Z"
id: 1981044
url: "https://selfcontroller.iteye.com/blog/1981044"
---

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

```js

    //首先定义一个事件处理函数:
    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，我们也可以这样用:

```js

   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'});


```