zoukankan      html  css  js  c++  java
  • JQuery事件绑定bind、live、on、trigger

    one

    作用:只触发一次,并在触发后失效,触发时会产生时间冒泡。

    语法:$(selector).one(event,data,function)

    例子:

    1 $(item).one("click", { param1: 1, param2: 2 }, function (event) {
    2     //阻止默认事件
    3     event.preventDefault();
    4     //阻止事件冒泡
    5     event.stopPropagation();
    6     console.log(event.data.param1 + event.data.param2);
    7 });

    bind

    作用:添加时间监听,不监听后续动态添加的dom。

    语法:$(selector).bind(event,data,function,map)

    例子:

     1 $(item).bind("click", { param1: 1, param2: 2 }, function (event) {
     2     //阻止默认事件
     3     event.preventDefault();
     4     //阻止事件冒泡
     5     event.stopPropagation();
     6     console.log(event.data.param1 + event.data.param2);
     7 },{
     8     "mouseenter": function(){},
     9     "mouseleave": function(){}
    10 });

    unbind

    作用:移除被选元素的事件处理程序,如果没有指定处理程序,会删除指定元素的所有事件处理程序。

    语法:$(selector).unbind(event,function)

    例子:

    1 $(item).unbind("click", showMe)

    live

    作用:添加时间监听,会监听后续动态添加的dom。

    语法:$(selector).live(event,data,function)

    例子:

    1 $(item).live("click", { param1: 1, param2: 2 }, function (event) {
    2     //阻止默认事件
    3     event.preventDefault();
    4     //阻止事件冒泡
    5     event.stopPropagation();
    6     console.log(event.data.param1 + event.data.param2);
    7 });

    delegate

    作用:指定某区域下的元素绑定事件,会监听后续动态添加的dom。

    语法:$(selector).delegate(childSelector,event,data,function)

    例子:

    1 $(fatherItem).delegate(item, "click", { param1: 1, param2: 2 }, function (event) {
    2     //阻止默认事件
    3     event.preventDefault();
    4     //阻止事件冒泡
    5     event.stopPropagation();
    6     console.log(event.data.param1 + event.data.param2);
    7 });

    on

    作用:自 jQuery 版本 1.7 起,on() 方法是 bind()、live() 和 delegate() 方法的新的推荐替代品,支持动态绑定。

    语法:$(selector).on(event,childSelector,data,function)

    例子:

     1 $(item).on("click", { param1: 1, param2: 2 }, function (event) {
     2     //阻止默认事件
     3     event.preventDefault();
     4     //阻止事件冒泡
     5     event.stopPropagation();
     6     console.log(event.data.param1 + event.data.param2);
     7 });
     8 $(fatherItem).on(item, "click", { param1: 1, param2: 2 }, function (event) {
     9     //阻止默认事件
    10     event.preventDefault();
    11     //阻止事件冒泡
    12     event.stopPropagation();
    13     console.log(event.data.param1 + event.data.param2);
    14 });

    off

    作用:自 jQuery 版本 1.7 起,off() 方法是 unbind()、die() 和 undelegate() 方法的新的推荐替代品

    语法:$(selector).off(event,selector,function(eventObj),map)

    例子:

    1 $(item).off("click", showMe);
    2 $(fatherItem).off(item, "click", showMe, {
    3     "mouseenter": showMe,
    4     "mouseleave": showMe
    5 });

    trigger

    作用:触发被选元素的指定事件类型,支持事件冒泡。

    语法:$(selector).trigger(event,[param1,param2,...])

    例子:

    1 $(item).trigger("click", param1, param2);

    triggerHandler

    作用:触发指定事件,屏蔽元素默认事件、阻止时间冒泡、返回事件返回值而不是jq对象、只触发匹配的第一个元素。

    语法:$(selector).triggerHandler(event,[param1,param2,...])

    例子:

    1 $(item).triggerHandler("click", param1, param2);
  • 相关阅读:
    leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
    ros topic 发布一次可能会接收不到数据
    python中的print()、str()和repr()的区别
    python 部分函数
    uiautomatorviewer错误 unable toconnect to adb
    pyqt 不规则形状窗口显示
    appium 计算器demo
    Spring 3.0 注解注入详解
    Spring Autowire自动装配
    restful 学习地址
  • 原文地址:https://www.cnblogs.com/guanghe/p/11357989.html
Copyright © 2011-2022 走看看