ドキュメント

Events/triggerHandler

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Events

triggerHandler( event, [data] )

Triggers all bound event handlers on an element (for a specific event type) WITHOUT executing the browser's default actions, bubbling, or live events.

This method behaves very similarly to the trigger method, with two major exceptions:

First, no default browser actions are triggered, the triggered event does not bubble, and live events aren't triggered.

Second, the event is only triggered on the first element within the jQuery collection. This method returns the return value of the triggered handler instead of a chainable jQuery object. Also, if the jQuery collection is empty, this method returns 'undefined'.

Arguments:
eventString,Event,Object
An event type to trigger.
data (Optional)Array
Additional data to pass as arguments (after the event object) to the event handler.
Examples:

If you called .triggerHandler() on a focus event - the browsers default focus action would not be triggered, only the event handlers bound to the focus event.

    $("#old").click(function(){
      $("input").trigger("focus");
    });
    $("#new").click(function(){
      $("input").triggerHandler("focus");
    });
    $("input").focus(function(){
      $("<span>Focused!</span>").appendTo("body").fadeOut(1000);
    });

<!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(){
    
    $("#old").click(function(){
      $("input").trigger("focus");
    });
    $("#new").click(function(){
      $("input").triggerHandler("focus");
    });
    $("input").focus(function(){
      $("<span>Focused!</span>").appendTo("body").fadeOut(1000);
    });

  });
  </script>
  
</head>
<body>
  <button id="old">.trigger("focus")</button>
  <button id="new">.triggerHandler("focus")</button><br/><br/>
  <input type="text" value="To Be Focused"/>
</body>
</html>

NameType