jQuery die()方法用法和介绍

2021年3月24日15:05:46 发表评论 756 次浏览

die()方法加上live()方法, 为选定的元素删除一个或多个事件处理程序。

语法如下:

$(selector).die(event, function)

参数:

  • 事件:指定要删除的一个或多个事件处理程序。多个有效事件值以空格分隔。
  • 功能:它用于指定要删除的功能。

示例1:

<!DOCTYPE html>
< html >
  
< head >
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" >
     </ script >
     < script >
         function changeSize() {
             $(this).animate({
                 fontSize: "+=3px"
             });
         }
  
         function changeSpacing() {
             $(this).animate({
                 letterSpacing: "+=2px"
             });
         }
  
         $(document).ready(function() {
             $("p").live("click", changeSize);
             $("p").live("click", changeSpacing);
             $("button").click(function() {
                 $("p").die("click", changeSize);
             });
         });
     </ script >
</ head >
  
< body >
  
     < center >
         < p style = "color:green;" >
           Geeks for geeks.
       </ p >
         < button >
           added with the live() method, Remove the event handler changeSize(), for p elements
       </ button >
     </ center >
</ body >
  
</ html >

输出如下:

在单击段落之前:

jQuery die()方法用法和介绍1

单击该段后:

jQuery die()方法用法和介绍2

示例2:

<!DOCTYPE html>
< html >
  
< head >
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" >
     </ script >
     < script >
         function changeSize() {
             $(this).animate({
                 fontSize: "+=3px"
             });
         }
  
         function changeSpacing() {
             $(this).animate({
                 letterSpacing: "+=2px"
             });
         }
  
         $(document).ready(function() {
             $("h1").live("click", changeSize);
             $("h1").live("click", changeSpacing);
             $("button").click(function() {
                 $("h1").die("click", changeSize);
             });
         });
     </ script >
</ head >
  
< body >
     < div >
         < center >
             < h1 >welcome to GFG</ h1 >
       </ center >
     </ div >
  
     < center >
         < button >click here</ button >
     </ center >
</ body >
  
</ html >

在单击段落之前:

jQuery die()方法用法和介绍3

单击该段后:

jQuery die()方法用法和介绍4

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: