ドキュメント

Events/die

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Events

die[type][fn] )

Added in jQuery 1.3: This does the opposite of live, it removes a bound live event.

Without any arguments, all bound live events are removed.

You can also unbind custom events registered with live.

If the type is provided, all bound live events of that type are removed.

If the function that was passed to live is provided as the second argument, only that specific event handler is removed.

Arguments:
type (Optional)String
A live event type to unbind.
fn (Optional)Function
A function to unbind from the event on each of the set of matched elements.
Examples:

Can bind and unbind events to the colored button.

    function aClick() {
      $("div").show().fadeOut("slow");
    }
    $("#bind").click(function () {
      $("#theone").live("click", aClick)
                  .text("Can Click!");
    });
    $("#unbind").click(function () {
      $("#theone").die("click", aClick)
                  .text("Does nothing...");
    });

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    function aClick() {
      $("div").show().fadeOut("slow");
    }
    $("#bind").click(function () {
      $("#theone").live("click", aClick)
                  .text("Can Click!");
    });
    $("#unbind").click(function () {
      $("#theone").die("click", aClick)
                  .text("Does nothing...");
    });

  });
  </script>
  <style>
  button { margin:5px; }
  button#theone { color:red; background:yellow; }
  </style>
</head>
<body>
  <button id="theone">Does nothing...</button>
  <button id="bind">Bind Click</button>
  <button id="unbind">Unbind Click</button>
  <div style="display:none;">Click!</div>
</body>
</html>

To unbind all live events from all paragraphs, write:

$("p").die()

To unbind all live click events from all paragraphs, write:

$("p").die( "click" )

To unbind just one previously bound handler, pass the function in as the second argument:

var foo = function () {
  // code to handle some kind of event
};

$("p").live("click", foo); // ... now foo will be called when paragraphs are clicked ...

$("p").die("click", foo); // ... foo will no longer be called.

NameType