Test

Page 216

Javascript $("form").bind("submit", function(event) { event.preventDefault(); });

Example 6:

Stop an event from bubbling without preventing the default action by using the .stopPropagation() method.

Javascript $("form").bind("submit", function(event) { event.stopPropagation(); });

Example 7:

Bind custom events.

Javascript

$("p").bind("myCustomEvent", function(e, myName, myValue){ $(this).text(myName + ", hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent", [ "John" ]); });

CSS p { color:red; } span { color:blue; }

HTML <p>Has an attached custom event.</p> <button>Trigger custom event</button> <span style="display:none;"></span>

Example 8:

Bind multiple events simultaneously.

Javascript $("div.test").bind({ click: function(){ $(this).addClass("active"); }, mouseenter: function(){ $(this).addClass("inside"); }, mouseleave: function(){ $(this).removeClass("inside"); } });


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.