jQuery 事件与应用

ready()事件
ready()事件类似于onLoad()事件,但前者只要页面的DOM结构加载后便触发,而后者必须在页面全部元素加载成功才触发,ready()可以写多个,按顺序执行。
此外,下列写法是相等的:
$(document).ready(function(){})等价于$(function(){});
例如:

<body>
  <h3>页面载入时触发ready()事件</h3>
  <div id="tip"></div>
  <input id="btntest" type="button" value="点下我" />
  <script type="text/javascript">
    $(function(){
      $("#btntest").bind("click", function () {
        $("#tip").html("我被点击了!");
      });
    });
  </script>
</body>

bind()方法
bind()方法绑定元素的事件非常方便,绑定前,需要知道被绑定的元素名,绑定的事件名称,事件中执行的函数内容就可以,它的绑定格式如下:
$(selector).bind(event,[data] function)
例如:

<body>
  <h3>bind()方法绑多个事件</h3>
  <input id="btntest" type="button" value="点击或移出就不可用了" />
  <script type="text/javascript">
    $(function () {
      $("#btntest").bind('click mouseout', function () {
        $(this).attr("disabled", "true");
      })
    });
  </script>
</body>

hover()
hover()方法的功能是当鼠标移到所选元素上时,执行方法中的第一个函数,鼠标移出时,执行方法中的第二个函数,实现事件的切实效果,调用格式如下:
$(selector).hover(over,out);
例如:

<body>
  <h3>hover()方法切换事件</h3>
  <div>别走!你就是土豪</div>
  <script type="text/javascript">
    $(function () {
      $('div').hover(
        function () {
          $(this).addClass("orange");
        },
        function () {
          $(this).removeClass("orange")
        }
      );
    });
  </script>
</body>

toggle()方法
toggle()方法可以在元素的click事件中绑定两个或两个以上的函数,同时,它还可以实现元素的隐藏与显示的切换,绑定多个函数的调用格式如下:
$(selector).toggle(fun1(),fun2(),funN(),…)
例如:

<body>
  <h3>toggle()方法绑定多个函数</h3>
  <input id="btntest" type="button" value="点一下我" />
  <div>我是动态显示的</div>
  <script type="text/javascript">
    $(function () {
      $("#btntest").bind("click", function () {
        $('div').toggle();
      })
    });
  </script>
</body>

unbind()方法
unbind()方法可以移除元素已绑定的事件,它的调用格式如下:
$(selector).unbind(event,fun)
其中参数event表示需要移除的事件名称,多个事件名用空格隔开,fun参数为事件执行时调用的函数名称。
例如:

<body>
  <h3>unbind()移除绑定的事件</h3>
  <input id="btntest" type="button" value="移除事件" />
  <div>土豪,咱们交个朋友吧</div>
  <script type="text/javascript">
    $(function () {
      $("div").bind("click",function () {
        $(this).removeClass("backcolor").addClass("color");
      }).bind("dblclick", function () {
        $(this).removeClass("color").addClass("backcolor");
      });
      $("#btntest").bind("click", function () {
        $('div').unbind(*);
        $(this).attr("disabled", "true");
      });
    });
  </script>
</body>

one()方法
one()方法可以绑定元素任何有效的事件,但这种方法绑定的事件只会触发一次,它的调用格式如下:
$(selector).one(event,[data],fun)
参数event为事件名称,data为触发事件时携带的数据,fun为触发该事件时执行的函数。
例如:

<body>
  <h3>one()方法执行一次绑定事件</h3>
  <div>请点击我一下</div>
  <script type="text/javascript">
    $(function () {
      var intI = 0;
      $('div').one("click", function () {
        intI++;
        $(this).css("font-size", intI + "px");
      })
    });
  </script>
</body>

trigger()方法
trigger()方法可以直接手动触发元素指定的事件,这些事件可以是元素自带事件,也可以是自定义的事件,总之,该事件必须能执行,它的调用格式为:
$(selector).trigger(event)
其中event参数为需要手动触发的事件名称。
例如:

<body>
  <h3>trigger()手动触发事件</h3>
  <div>土豪,咱们交个朋友吧</div>
  <script type="text/javascript">
    $(function () {
      $("div").bind("change-color", function () {
        $(this).addClass("color");
      });
      $('div').trigger("change-color");
    });
  </script>
</body>

focus和blur事件
focus事件在元素获取焦点时触发,如点击文本框时,触发该事件;而blur事件则在元素丢失焦点时触发,如点击除文本框的任何元素,都会触发该事件。
例如:

<body>
  <h3>表单中文本框的focus和blur事件</h3>
  <input id="txtest" type="text" value="" />
  <div></div>
  <script type="text/javascript">
    $(function () {
      $("input").bind("focus", function () {
        $("div").html("请输入您的姓名!");
      }).bind("blur", function () {
        if ($(this).val().length == 0)
          $("div").html("你的名称不能为空!");
      })
    });
  </script>
</body>

change事件
当一个元素的值发生变化时,将会触发change事件,例如在选择下拉列表框中的选项时,就会触change事件。
例如:

<body>
  <h3>下拉列表的change事件</h3>
  <select id="seltest">
    <option value="葡萄">葡萄</option>
    <option value="苹果">苹果</option>
    <option value="荔枝">荔枝</option>
    <option value="香焦">香焦</option>
  </select>
  <script type="text/javascript">
    $(function () {
      $('#seltest').bind("change", function () {
        if ($(this).val() == "苹果")
          $(this).css("background-color", "red");
        else
          $(this).css("background-color", "green");
      })
    });
  </script>
</body>

live()方法
与bind()方法相同,live()方法与可以绑定元素的可执行事件,除此相同功能之外,live()方法还可以绑定动态元素,即使用代码添加的元素事件,格式如下:
$(selector).live(event,[data],fun)
参数event为事件名称,data为触发事件时携带的数据,fun为触发该事件时执行的函数。
例如:

<body>
  <h3>live()方法绑多个事件</h3>
  <script type="text/javascript">
    $(function () {
      $('#btntest').live("click mouseout", function () {
        $(this).attr("disabled", "true");
      })
      $("body").append("<input id='btntest' type='button' value='点击或移出就不可用了' />");
    });
  </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值