UNPKG

typescript-closure-tools

Version:

Command-line tools to convert closure-style JSDoc annotations to typescript, and to convert typescript sources to closure externs files

1,678 lines (1,541 loc) 97 kB
/// <reference path="jquery.d.ts" /> function test_add() { $("p").add("div").addClass("widget"); var pdiv = $("p").add("div"); $('li').add('p').css('background-color', 'red'); $('li').add(document.getElementsByTagName('p')[0]) .css('background-color', 'red'); $('li').add('<p id="new">new paragraph</p>') .css('background-color', 'red'); $("div").css("border", "2px solid red") .add("p") .css("background", "yellow"); $("p").add("span").css("background", "yellow"); $("p").clone().add("<span>Again</span>").appendTo(document.body); $("p").add(document.getElementById("a")).css("background", "yellow"); var collection = $("p"); collection = collection.add(document.getElementById("a")); collection.css("background", "yellow"); } function test_addClass() { $("p").addClass("myClass yourClass"); $("p").removeClass("myClass noClass").addClass("yourClass"); $("ul li:last").addClass(function (index) { return "item-" + index; }); $("p:last").addClass("selected"); $("p:last").addClass("selected highlight"); $("div").addClass(function (index, currentClass) { var addedClass: string; if (currentClass === "red") { addedClass = "green"; $("p").text("There is one green div"); } return addedClass; }); } function test_after() { $('.inner').after('<p>Test</p>'); $('<div/>').after('<p></p>'); $('<div/>').after('<p></p>').addClass('foo') .filter('p').attr('id', 'bar').html('hello') .end() .appendTo('body'); $('p').after(function () { return '<div>' + this.className + '</div>'; }); var $newdiv1 = $('<div id="object1"/>'), newdiv2 = document.createElement('div'), existingdiv1 = document.getElementById('foo'); $('p').first().after($newdiv1, [newdiv2, existingdiv1]); $("p").after(document.createTextNode("Hello")); $("p").after($("b")); } function test_ajax() { $.ajax({ url: "test.html", context: document.body }).done(function () { $(this).addClass("done"); }); $.ajax({ statusCode: { 404: function () { alert("page not found"); } } }); $.ajax({ url: "http://fiddle.jshell.net/favicon.png", beforeSend: function (xhr) { xhr.overrideMimeType("text/plain; charset=x-user-defined"); } }).done(function (data) { if (console && console.log) { console.log("Sample of data:", data.slice(0, 100)); } }); $.ajax({ url: 'ajax/test.html', success: function (data) { $('.result').html(data); alert('Load was performed.'); } }); var _super = jQuery.ajaxSettings.xhr; jQuery.ajaxSettings.xhr = function () { var xhr = _super(), getAllResponseHeaders = xhr.getAllResponseHeaders; xhr.getAllResponseHeaders = function () { if (getAllResponseHeaders()) { return getAllResponseHeaders(); } var allHeaders = ""; $(["Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma"]).each(function (i, header_name) { if (xhr.getResponseHeader(header_name)) { allHeaders += header_name + ": " + xhr.getResponseHeader(header_name) + "\n"; } return allHeaders; }); }; return xhr; }; $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function (msg) { alert("Data Saved: " + msg); }); $.ajax({ url: "test.html", cache: false }).done(function (html) { $("#results").append(html); }); var xmlDocument = []; var xmlRequest = $.ajax({ url: "page.php", processData: false, data: xmlDocument }); var handleResponse; xmlRequest.done(handleResponse); var menuId = $("ul.nav").first().attr("id"); var request = $.ajax({ url: "script.php", type: "POST", data: { id: menuId }, dataType: "html" }); request.done(function (msg) { $("#log").html(msg); }); request.fail(function (jqXHR, textStatus) { alert("Request failed: " + textStatus); }); $.ajax({ type: "GET", url: "test.js", dataType: "script" }); } function test_ajaxComplete() { $('.log').ajaxComplete(function () { $(this).text('Triggered ajaxComplete handler.'); }); $('.trigger').click(function () { $('.result').load('ajax/test.html'); }); $('.log').ajaxComplete(function (e, xhr, settings) { if (settings.url == 'ajax/test.html') { $(this).text('Triggered ajaxComplete handler. The result is ' + xhr.responseText); } }); $("#msg").ajaxComplete(function (event, request, settings) { $(this).append("<li>Request Complete.</li>"); }); } function test_ajaxError() { $("div.log").ajaxError(function () { $(this).text("Triggered ajaxError handler."); }); $("button.trigger").click(function () { $("div.result").load("ajax/missing.html"); }); $("div.log").ajaxError(function (e, jqxhr, settings, exception) { if (settings.url == "ajax/missing.html") { $(this).text("Triggered ajaxError handler."); } }); $("#msg").ajaxError(function (event, request, settings) { $(this).append("<li>Error requesting page " + settings.url + "</li>"); }); } function test_ajaxPrefilter() { var currentRequests = {}; $.ajaxPrefilter(function (options, originalOptions, jqXHR) { if (options.abortOnRetry) { if (currentRequests[options.url]) { currentRequests[options.url].abort(); } currentRequests[options.url] = jqXHR; } }); $.ajaxPrefilter(function (options) { if (options.crossDomain) { options.url = "http://mydomain.net/proxy/" + encodeURIComponent(options.url); options.crossDomain = false; } }); $.ajaxPrefilter("json script", function (options, originalOptions, jqXHR) { }); var isActuallyScript; $.ajaxPrefilter(function (options) { if (isActuallyScript(options.url)) { return "script"; } }); } function test_ajaxSend() { $('.log').ajaxSend(function () { $(this).text('Triggered ajaxSend handler.'); }); $('.trigger').click(function () { $('.result').load('ajax/test.html'); }); $('.log').ajaxSend(function (e, jqxhr, settings) { if (settings.url == 'ajax/test.html') { $(this).text('Triggered ajaxSend handler.'); } }); $("#msg").ajaxSend(function (evt, request, settings) { $(this).append("<li>Starting request at " + settings.url + "</li>"); }); } function test_ajaxSetup() { $.ajaxSetup({ url: 'ping.php' }); $.ajax({ data: { 'name': 'Dan' } }); $.ajaxSetup({ url: "/xmlhttp/", global: false, type: "POST" }); } function test_ajaxStart() { $('.log').ajaxStart(function () { $(this).text('Triggered ajaxStart handler.'); }); $('.trigger').click(function () { $('.result').load('ajax/test.html'); }); $("#loading").ajaxStart(function () { $(this).show(); }); } function test_ajaxStop() { $('.log').ajaxStop(function () { $(this).text('Triggered ajaxStop handler.'); }); $('.trigger').click(function () { $('.result').load('ajax/test.html'); }); $("#loading").ajaxStop(function () { $(this).hide(); }); } function test_ajaxSuccess() { $('.log').ajaxSuccess(function () { $(this).text('Triggered ajaxSuccess handler.'); }); $('.trigger').click(function () { $('.result').load('ajax/test.html'); }); $('.log').ajaxSuccess(function (e, xhr, settings) { if (settings.url == 'ajax/test.html') { $(this).text('Triggered ajaxSuccess handler. The ajax response was:' + xhr.responseText); } }); $("#msg").ajaxSuccess(function (evt, request, settings) { $(this).append("<li>Successful Request!</li>"); }); } function test_allSelector() { var elementCount = $("*").css("border", "3px solid red").length; $("body").prepend("<h3>" + elementCount + " elements found</h3>"); var elementCount2 = $("#test").find("*").css("border", "3px solid red").length; $("body").prepend("<h3>" + elementCount2 + " elements found</h3>"); } function test_animate() { $('#clickme').click(function () { $('#book').animate({ opacity: 0.25, left: '+=50', height: 'toggle' }, 5000, function () { }); }); $('li').animate({ opacity: .5, height: '50%' }, { step: function (now, fx) { var data = fx.elem.id + ' ' + fx.prop + ': ' + now; $('body').append('<div>' + data + '</div>'); } }); $('#clickme').click(function () { $('#book').animate({ width: ['toggle', 'swing'], height: ['toggle', 'swing'], opacity: 'toggle' }, 5000, 'linear', function () { $(this).after('<div>Animation complete.</div>'); }); }); $('#clickme').click(function () { $('#book').animate({ width: 'toggle', height: 'toggle' }, { duration: 5000, specialEasing: { width: 'linear', height: 'easeOutBounce' }, complete: function () { $(this).after('<div>Animation complete.</div>'); } }); }); $("#go").click(function () { $("#block").animate({ width: "70%", opacity: 0.4, marginLeft: "0.6in", fontSize: "3em", borderWidth: "10px" }, 1500); }); $("#right").click(function () { $(".block").animate({ "left": "+=50px" }, "slow"); }); $("#left").click(function () { $(".block").animate({ "left": "-=50px" }, "slow"); }); $("#go1").click(function () { $("#block1").animate({ width: "90%" }, { queue: false, duration: 3000 }) .animate({ fontSize: "24px" }, 1500) .animate({ borderRightWidth: "15px" }, 1500); }); $("#go2").click(function () { $("#block2").animate({ width: "90%" }, 1000) .animate({ fontSize: "24px" }, 1000) .animate({ borderLeftWidth: "15px" }, 1000); }); $("#go3").click(function () { $("#go1").add("#go2").click(); }); $("#go4").click(function () { $("div").css({ width: "", fontSize: "", borderWidth: "" }); }); $("#go").click(function () { $(".block:first").animate({ left: 100 }, { duration: 1000, step: function (now, fx) { $(".block:gt(0)").css("left", now); } }); }); $("p").animate({ height: "toggle", opacity: "toggle" }, "slow"); $("p").animate({ left: 50, opacity: 1 }, 500); $("p").animate({ left: "50px", opacity: 1 }, { duration: 500, queue: false }); $("p").animate({ opacity: "show" }, "slow", "easein"); $("p").animate({ height: "toggle", opacity: "toggle" }, { duration: "slow" }); $("p").animate({ opacity: "show" }, { duration: "slow", easing: "easein" }); $("p").animate({ height: 200, width: 400, opacity: 0.5 }, 1000, "linear", function () { alert("all done"); }); } function test_animatedSelector() { $("#run").click(function () { $("div:animated").toggleClass("colored"); }); function animateIt() { $("#mover").slideToggle("slow", animateIt); } animateIt(); } function test_slideToggle() { $("button").click(function () { $("p").slideToggle("slow"); }); $("#aa").click(function () { $("div:not(.still)").slideToggle("slow", function () { var n = parseInt($("span").text(), 10); $("span").text(n + 1); }); }); } function test_toggle() { $(".target").toggle(); $("#clickme").click(function () { $("#book").toggle("slow", function () { // Animation complete. }); }); $("#foo").toggle(true); $("button").click(function () { $("p").toggle(); }); $("button").click(function () { $("p").toggle("slow"); }); var flip = 0; $("button").click(function () { $("p").toggle(flip++ % 2 === 0); }); } function test_append() { $('.inner').append('<p>Test</p>'); $('.container').append($('h2')); var $newdiv1 = $('<div id="object1"/>'), newdiv2 = document.createElement('div'), existingdiv1 = document.getElementById('foo'); $('body').append($newdiv1, [newdiv2, existingdiv1]); } function test_appendTo() { $('<p>Test</p>').appendTo('.inner'); $('h2').appendTo($('.container')); } function test_attr() { var title = $("em").attr("title"); $("div").text(title); $('#greatphoto').attr('alt', 'Beijing Brush Seller'); $('#greatphoto') .attr('title', 'Photo by Kelly Clark'); $('#greatphoto').attr({ alt: 'Beijing Brush Seller', title: 'photo by Kelly Clark' }); $('#greatphoto').attr('title', function (i, val) { return val + ' - photo by Kelly Clark' }); $("div").attr("id", function (arr) { return "div-id" + arr; }) .each(function () { $("span", this).html("(ID = '<b>" + this.id + "</b>')"); }); $("img").attr("src", function () { return "/images/" + this.title; }); } function test_attributeSelectors() { $('a[hreflang|="en"]').css('border', '3px dotted green'); $('input[name*="man"]').val('has man in it!'); $('input[name~="man"]').val('mr. man is in it!'); $('input[name$="letter"]').val('a letter'); $('input[value="Hot Fuzz"]').next().text(" Hot Fuzz"); $('input[name!="newsletter"]').next().append('<b>; not newsletter</b>'); $('input[name^="news"]').val('news here!'); } function test_before() { $('.inner').before('<p>Test</p>'); $('.container').before($('h2')); $("<div/>").before("<p></p>"); var $newdiv1 = $('<div id="object1"/>'), newdiv2 = document.createElement('div'), existingdiv1 = document.getElementById('foo'); $('p').first().before($newdiv1, [newdiv2, existingdiv1]); } function test_bind() { $('#foo').bind('click', function () { alert('User clicked on "foo."'); }); $('#foo').bind('mouseenter mouseleave', function () { $(this).toggleClass('entered'); }); $('#foo').bind({ click: function () { }, mouseenter: function () { } }); $('#foo').bind('click', function () { alert($(this).text()); }); $(document).ready(function () { $('#foo').bind('click', function (event) { alert('The mouse cursor is at (' + event.pageX + ', ' + event.pageY + ')'); }); }); var message = 'Spoon!'; $('#foo').bind('click', function () { alert(message); }); message = 'Not in the face!'; $('#bar').bind('click', function () { alert(message); }); var message = 'Spoon!'; $('#foo').bind('click', { msg: message }, function (event) { alert(event.data.msg); }); message = 'Not in the face!'; $('#bar').bind('click', { msg: message }, function (event) { alert(event.data.msg); }); $("p").bind("click", function (event) { var str = "( " + event.pageX + ", " + event.pageY + " )"; $("span").text("Click happened! " + str); }); $("p").bind("dblclick", function () { $("span").text("Double-click happened in " + this.nodeName); }); $("p").bind("mouseenter mouseleave", function (event) { $(this).toggleClass("over"); }); $("p").bind("click", function () { alert($(this).text()); }); function handler(event) { alert(event.data.foo); } $("p").bind("click", { foo: "bar" }, handler) $("form").bind("submit", function () { return false; }) $("form").bind("submit", function (event) { event.preventDefault(); }); $("form").bind("submit", function (event) { event.stopPropagation(); }); $("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"]); }); $("div.test").bind({ click: function () { $(this).addClass("active"); }, mouseenter: function () { $(this).addClass("inside"); }, mouseleave: function () { $(this).removeClass("inside"); } }); } function test_unbind() { $("#foo").unbind(); $("#foo").unbind("click"); var handler = function () { alert("The quick brown fox jumps over the lazy dog."); }; $("#foo").bind("click", handler); $("#foo").unbind("click", handler); $("#foo").bind("click", function () { alert("The quick brown fox jumps over the lazy dog."); }); // Will NOT work $("#foo").unbind("click", function () { alert("The quick brown fox jumps over the lazy dog."); }); $("#foo").bind("click.myEvents", handler); $("#foo").unbind("click"); $("#foo").unbind("click.myEvents"); $("#foo").unbind(".myEvents"); var timesClicked = 0; $("#foo").bind("click", function (event) { alert("The quick brown fox jumps over the lazy dog."); timesClicked++; if (timesClicked >= 3) { $(this).unbind(event); } }); function aClick() { $("div").show().fadeOut("slow"); } $("#bind").click(function () { $("#theone") .bind("click", aClick) .text("Can Click!"); }); $("#unbind").click(function () { $("#theone") .unbind("click", aClick) .text("Does nothing..."); }); $("p").unbind(); $("p").unbind("click"); var foo = function () { // Code to handle some kind of event }; $("p").bind("click", foo); // ... Now foo will be called when paragraphs are clicked ... $("p").unbind("click", foo); // ... foo will no longer be called. } function test_blur() { $('#target').blur(function () { alert('Handler for .blur() called.'); }); $('#other').click(function () { $('#target').blur(); }); $("p").blur(); } interface JQueryStatic { Topic; } function test_callbacks() { function fn1(value) { console.log(value); } function fn2(value) { fn1("fn2 says:" + value); return false; } var callbacks = $.Callbacks(); var callbacks2 = $.Callbacks("once"); callbacks.add(fn1); callbacks.fire("foo!"); callbacks.add(fn2); callbacks.fire("bar!"); callbacks.remove(fn2); callbacks.fire("foobar"); var topics = {}; jQuery.Topic = function (id) { var callbacks, method, topic = id && topics[id]; if (!topic) { callbacks = jQuery.Callbacks(); topic = { publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove }; if (id) { topics[id] = topic; } } return topic; }; $.Topic("mailArrived").subscribe(fn1); $.Topic("mailArrived").subscribe(fn2); $.Topic("mailSent").subscribe(fn1); $.Topic("mailArrived").publish("hello world!"); $.Topic("mailSent").publish("woo! mail!"); $.Topic("mailArrived").subscribe(fn1); var dfd = $.Deferred(); var topic = $.Topic("mailArrived"); dfd.done(topic.publish); dfd.resolve("its been published!"); } function test_callbacksFunctions() { var foo = function (value) { console.log('foo:' + value); } var bar = function (value) { console.log('bar:' + value); } var callbacks = $.Callbacks(); callbacks.add(foo); callbacks.fire('hello'); callbacks.add(bar); callbacks.fire('world'); callbacks.disable(); // Test the disabled state of the list console.log(callbacks.disabled()); // Outputs: true callbacks.empty(); callbacks.fire('hello'); console.log(callbacks.fired()); callbacks.fireWith(window, ['foo', 'bar']); var foo2 = function (value1, value2) { console.log('Received:' + value1 + ',' + value2); }; console.log(callbacks.has(foo2)); callbacks.lock(); console.log(callbacks.locked()); callbacks.remove(foo); } function test_change() { $('.target').change(function () { alert('Handler for .change() called.'); }); $('#other').click(function () { $('.target').change(); }); $("input[type='text']").change(function () { }); $("input[type='text']").change(); } function test_children() { $('ul.level-2').children().css('background-color', 'red'); $("#container").click(function (e) { $("*").removeClass("hilite"); var $kids = $(e.target).children(); var len = $kids.addClass("hilite").length; $("#results span:first").text(len); //$("#results span:last").text(e.target.tagName); e.preventDefault(); return false; }); $("div").children(".selected").css("color", "blue"); } function test_clearQueue() { $("#start").click(function () { var myDiv = $("div"); myDiv.show("slow"); myDiv.animate({ left: '+=200' }, 5000); myDiv.queue(function () { var _this = $(this); _this.addClass("newcolor"); _this.dequeue(); }); myDiv.animate({ left: '-=200' }, 1500); myDiv.queue(function () { var _this = $(this); _this.removeClass("newcolor"); _this.dequeue(); }); myDiv.slideUp(); }); $("#stop").click(function () { var myDiv = $("div"); myDiv.clearQueue(); myDiv.stop(); }); } function test_click() { $("#target").click(function () { alert("Handler for .click() called."); }); $("#other").click(function () { $("#target").click(); }); $("p").click(function () { $(this).slideUp(); }); $("p").click(); } function test_submit() { $("#target").submit(function () { alert("Handler for .submit() called."); }); $("#target").submit(); } function test_trigger() { $("#foo").on("click", function () { alert($(this).text()); }); $("#foo").trigger("click"); $("#foo").on("custom", function (event, param1?, param2?) { alert(param1 + "\n" + param2); }); $("#foo").trigger("custom", ["Custom", "Event"]); $("button:first").click(function () { update($("span:first")); }); $("button:last").click(function () { $("button:first").trigger("click"); update($("span:last")); }); function update(j) { var n = parseInt(j.text(), 10); j.text(n + 1); } $("form:first").trigger("submit"); var event = jQuery.Event("submit"); $("form:first").trigger(event); if (event.isDefaultPrevented()) { // Perform an action... } $("p") .click(function (event, a, b) { // When a normal click fires, a and b are undefined // for a trigger like below a refers to "foo" and b refers to "bar" }) .trigger("click", ["foo", "bar"]); var event = jQuery.Event("logged"); (<any>event).user = "foo"; (<any>event).pass = "bar"; $("body").trigger(event); // Adapted from jQuery documentation which may be wrong on this occasion var event2 = jQuery.Event("logged"); $("body").trigger(event2, { type: "logged", user: "foo", pass: "bar" }); } function test_clone() { $('.hello').clone().appendTo('.goodbye'); var $elem = $('#elem').data({ "arr": [1] }), $clone = $elem.clone(true) .data("arr", $.extend([], $elem.data("arr"))); $("b").clone().prependTo("p"); $('#copy').append($('#orig .elem') .clone() .children('a') .prepend('foo - ') .parent() .clone()); } function test_prependTo() { $("<p>Test</p>").prependTo(".inner"); $("h2").prependTo($(".container")); $("span").prependTo("#foo"); } function test_closest() { $('li.item-a').closest('ul') .css('background-color', 'red'); $('li.item-a').closest('li') .css('background-color', 'red'); var listItemII = document.getElementById('ii'); $('li.item-a').closest('ul', listItemII) .css('background-color', 'red'); $('li.item-a').closest('#one', listItemII) .css('background-color', 'green'); $(document).bind("click", function (e) { $(e.target).closest("li").toggleClass("hilight"); }); var $listElements = $("li").css("color", "blue"); $(document).bind("click", function (e) { //$(e.target).closest($listElements).toggleClass("hilight"); }); } function test_contains() { jQuery.contains(document.documentElement, document.body); jQuery.contains(document.body, document.documentElement); } function test_contents() { $('.container').contents().filter(function () { return this.nodeType == 3; }) .wrap('<p></p>') .end() .filter('br') .remove(); $("#frameDemo").contents().find("a").css("background-color", "#BADA55"); } function test_context() { $("ul") .append("<li>" + $("ul").context + "</li>") .append("<li>" + $("ul", document.body).context.nodeName + "</li>"); } function test_css() { $("div").click(function () { var color = $(this).css("background-color"); $("#result").html("That div is <span style='color:" + color + ";'>" + color + "</span>."); }); $('div.example').css('width', function (index) { return index * 50; }); $("p").mouseover(function () { $(this).css("color", "red"); }); $("#box").one("click", function () { $(this).css("width", "+=200"); }); var words = $("p:first").text().split(" "); var text = words.join("</span> <span>"); $("p:first").html("<span>" + text + "</span>"); $("span").click(function () { $(this).css("background-color", "yellow"); }); $("p").hover(function () { $(this).css({ 'background-color': 'yellow', 'font-weight': 'bolder' }); }, function () { var cssObj = { 'background-color': '#ddd', 'font-weight': '', 'color': 'rgb(0,40,244)' } $(this).css(cssObj); }); $("div").click(function () { $(this).css({ width: function (index, value) { return parseFloat(value) * 1.2; }, height: function (index, value) { return parseFloat(value) * 1.2; } }); }); var dims = $("#box").css([ "width", "height", "backgroundColor" ]); } function test_cssHooks() { if (!$.cssHooks) { throw ("jQuery 1.4.3 or above is required for this plugin to work"); return; } $.cssHooks["someCSSProp"] = { get: function (elem, computed, extra) { }, set: function (elem, value) { } }; function styleSupport(prop) { var vendorProp, supportedProp, capProp = prop.charAt(0).toUpperCase() + prop.slice(1), prefixes = ["Moz", "Webkit", "O", "ms"], div = document.createElement("div"); if (prop in div.style) { supportedProp = prop; } else { for (var i = 0; i < prefixes.length; i++) { vendorProp = prefixes[i] + capProp; if (vendorProp in div.style) { supportedProp = vendorProp; break; } } } div = null; $.support[prop] = supportedProp; return supportedProp; } styleSupport("borderRadius"); $.cssNumber["someCSSProp"] = true; $.fx.step["someCSSProp"] = function (fx) { $.cssHooks["someCSSProp"].set(fx.elem, fx.now + fx.unit); }; } function test_data() { $('body').data('foo', 52); $('body').data('bar', { myType: 'test', count: 40 }); $('body').data('foo'); $('body').data(); $("div").data("test", { first: 16, last: "pizza!" }); $("span:first").text($("div").data("test").first); $("span:last").text($("div").data("test").last); alert($('body').data('foo')); alert($('body').data()); alert($("body").data("foo")); $("body").data("bar", "foobar"); alert($("body").data("bar")); $("div").data("role") === "page"; $("div").data("lastValue") === 43; $("div").data("hidden") === true; $("div").data("options").name === "John"; var value; switch ($("button").index(this)) { case 0: value = $("div").data("blah"); break; case 1: $("div").data("blah", "hello"); value = "Stored!"; break; case 2: $("div").data("blah", 86); value = "Stored!"; break; case 3: $("div").removeData("blah"); value = "Removed!"; break; } $("span").text("" + value); jQuery.data(document.body, 'foo', 52); jQuery.data(document.body, 'bar', 'test'); var div = $("div")[0]; jQuery.data(div, "test", { first: 16, last: "pizza!" }); $("span:first").text(jQuery.data(div, "test").first); $("span:last").text(jQuery.data(div, "test").last); $.data(document.getElementById("id"), "", 8).toFixed(2); $.data(document.getElementById("id"), "", "8").toUpperCase(); } function test_removeData() { $("span:eq(0)").text("" + $("div").data("test1")); $("div").data("test1", "VALUE-1"); $("div").data("test2", "VALUE-2"); $("span:eq(1)").text("" + $("div").data("test1")); $("div").removeData("test1"); $("span:eq(2)").text("" + $("div").data("test1")); $("span:eq(3)").text("" + $("div").data("test2")); } function test_jQuery_removeData() { var div = $("div")[0]; $("span:eq(0)").text("" + $("div").data("test1")); jQuery.data(div, "test1", "VALUE-1"); jQuery.data(div, "test2", "VALUE-2"); $("span:eq(1)").text("" + jQuery.data(div, "test1")); jQuery.removeData(div, "test1"); $("span:eq(2)").text("" + jQuery.data(div, "test1")); $("span:eq(3)").text("" + jQuery.data(div, "test2")); } function test_dblclick() { $('#target').dblclick(function () { alert('Handler for .dblclick() called.'); }); $('#other').click(function () { $('#target').dblclick(); }); $("p").dblclick(function () { alert("Hello World!"); }); var divdbl = $("div:first"); divdbl.dblclick(function () { divdbl.toggleClass('dbl'); }); $('#target').dblclick(); } function test_delay() { $('#foo').slideUp(300).delay(800).fadeIn(400); $("button").click(function () { $("div.first").slideUp(300).delay(800).fadeIn(400); $("div.second").slideUp(300).fadeIn(400); }); } function test_delegate() { $("table").delegate("td", "click", function () { $(this).toggleClass("chosen"); }); $("table").on("click", "td", function () { $(this).toggleClass("chosen"); }); $("body").delegate("p", "click", function () { $(this).after("<p>Another paragraph!</p>"); }); $("body").delegate("p", "click", function () { alert($(this).text()); }); $("body").delegate("a", "click", function () { return false; }); $("body").delegate("a", "click", function (event) { event.preventDefault(); }); $("body").delegate("p", "myCustomEvent", function (e, myName?, myValue?) { $(this).text("Hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent"); }); } function test_undelegate() { function aClick() { $("div").show().fadeOut("slow"); } $("#bind").click(function () { $("body") .delegate("#theone", "click", aClick) .find("#theone").text("Can Click!"); }); $("#unbind").click(function () { $("body") .undelegate("#theone", "click", aClick) .find("#theone").text("Does nothing..."); }); $("p").undelegate(); $("p").undelegate("click"); var foo = function () { // Code to handle some kind of event }; // ... Now foo will be called when paragraphs are clicked ... $("body").delegate("p", "click", foo); // ... foo will no longer be called. $("body").undelegate("p", "click", foo); var foo = function () { // Code to handle some kind of event }; // Delegate events under the ".whatever" namespace $("form").delegate(":button", "click.whatever", foo); $("form").delegate("input[type='text'] ", "keypress.whatever", foo); // Unbind all events delegated under the ".whatever" namespace $("form").undelegate(".whatever"); } function test_dequeue() { $("button").click(function () { $("div").animate({ left: '+=200px' }, 2000); $("div").animate({ top: '0px' }, 600); $("div").queue(function () { $(this).toggleClass("red"); $(this).dequeue(); }); $("div").animate({ left: '10px', top: '30px' }, 700); }); } function test_queue() { $("#show").click(function () { var n = jQuery.queue($("div")[0], "fx"); $("span").text("Queue length is: " + n.length); }); function runIt() { $("div") .show("slow") .animate({ left: "+=200" }, 2000) .slideToggle(1000) .slideToggle("fast") .animate({ left: "-=200" }, 1500) .hide("slow") .show(1200) .slideUp("normal", runIt); } runIt(); $(document.body).click(function () { var divs = $("div") .show("slow") .animate({ left: "+=200" }, 2000); jQuery.queue(divs[0], "fx", function () { $(this).addClass("newcolor"); jQuery.dequeue(this); }); divs.animate({ left: "-=200" }, 500); jQuery.queue(divs[0], "fx", function () { $(this).removeClass("newcolor"); jQuery.dequeue(this); }); divs.slideUp(); }); $("#start").click(function () { var divs = $("div") .show("slow") .animate({ left: "+=200" }, 5000); jQuery.queue(divs[0], "fx", function () { $(this).addClass("newcolor"); jQuery.dequeue(this); }); divs.animate({ left: "-=200" }, 1500); jQuery.queue(divs[0], "fx", function () { $(this).removeClass("newcolor"); jQuery.dequeue(this); }); divs.slideUp(); }); $("#stop").click(function () { jQuery.queue($("div")[0], "fx", []); $("div").stop(); }); } function test_detach() { $("p").click(function () { $(this).toggleClass("off"); }); var p; $("button").click(function () { if (p) { p.appendTo("body"); p = null; } else { p = $("p").detach(); } }); } function test_each() { $.each([52, 97], function (index, value) { alert(index + ': ' + value); }); var map = { 'flammable': 'inflammable', 'duh': 'no duh' }; $.each(map, function (key, value) { alert(key + ': ' + value); }); var arr = ["one", "two", "three", "four", "five"]; var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 }; jQuery.each(arr, function () { $("#" + this).text("Mine is " + this + "."); return (this != "three"); }); jQuery.each(obj, function (i, val) { $("#" + i).append(document.createTextNode(" - " + val)); }); $.each(['a', 'b', 'c'], function (i, l) { alert("Index #" + i + ": " + l); }); $.each({ name: "John", lang: "JS" }, function (k, v) { alert("Key: " + k + ", Value: " + v); }); $.each([{a: 1}, {a: 2}, {a: 3}], function (i, o) { alert("Index #" + i + ": " + o.a); }); $('li').each(function (index) { alert(index + ': ' + $(this).text()); }); $(document.body).click(function () { $("div").each(function (i) { if (this.style.color != "blue") { this.style.color = "blue"; } else { this.style.color = ""; } }); }); $("span").click(function () { $("li").each(function () { $(this).toggleClass("example"); }); }); $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); }); } function test_empty() { $('.hello').empty(); } function test_end() { $('ul.first').find('.foo').css('background-color', 'red') .end().find('.bar').css('background-color', 'green'); $('ul.first').find('.foo') .css('background-color', 'red') .end().find('.bar') .css('background-color', 'green') .end(); } function test_eq() { $('li').eq(2).css('background-color', 'red'); $('li').eq(-2).css('background-color', 'red'); $('li').eq(5).css('background-color', 'red'); $("body").find("div").eq(2).addClass("blue"); } function test_error() { $('#book') .error(function () { alert('Handler for .error() called.') }) .attr("src", "missing.png"); $("img") .error(function () { $(this).hide(); }) .attr("src", "missing.png"); jQuery.error = (message?: string) => { console.error(message); return this; } } function test_eventParams() { $("p").click(function (event) { event.currentTarget === this; }); $(".box").on("click", "button", function (event) { $(event.delegateTarget).css("background-color", "red"); }); $("a").click(function (event) { event.isDefaultPrevented(); event.preventDefault(); event.isDefaultPrevented(); }); function immediatePropStopped(e) { var msg = ""; if (e.isImmediatePropagationStopped()) { msg = "called" } else { msg = "not called"; } $("#stop-log").append("<div>" + msg + "</div>"); } $("button").click(function (event) { immediatePropStopped(event); event.stopImmediatePropagation(); immediatePropStopped(event); }); function propStopped(e) { var msg = ""; if (e.isPropagationStopped()) { msg = "called" } else { msg = "not called"; } $("#stop-log").append("<div>" + msg + "</div>"); } $("button").click(function (event) { propStopped(event); event.stopPropagation(); propStopped(event); }); $("p").bind("test.something", function (event) { alert(event.namespace); }); $("button").click(function (event) { $("p").trigger("test.something"); }); $(document).bind('mousemove', function (e) { $("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY); }); $("a").click(function (event) { event.preventDefault(); $('<div/>') .append('default ' + event.type + ' prevented') .appendTo('#log'); }); $("a").mouseout(function (event) { alert(event.relatedTarget.nodeName); }); $("button").click(function (event) { return "hey"; }); $("button").click(function (event) { $("p").html(event.result); }); $("p").click(function (event) { event.stopImmediatePropagation(); }); $("p").click(function (event) { $(this).css("background-color", "#f00"); }); $("div").click(function (event) { $(this).css("background-color", "#f00"); }); $("p").click(function (event) { event.stopPropagation(); }); $("body").click(function (event) { //bugfix, duplicate identifier. see: http://stackoverflow.com/questions/14824143/duplicate-identifier-nodename-in-jquery-d-ts //$("#log").html("clicked: " + event.target.nodeName); }); $('#whichkey').bind('keydown', function (e) { $('#log').html(e.type + ': ' + e.which); }); $('#whichkey').bind('mousedown', function (e) { $('#log').html(e.type + ': ' + e.which); }); } function test_extend() { var object1 = { apple: 0, banana: { weight: 52, price: 100 }, cherry: 97 }; var object2 = { banana: { price: 200 }, durian: 100 }; $.extend(object1, object2); var printObj = typeof JSON != "undefined" ? JSON.stringify : function (obj) { var arr = []; $.each(obj, function (key, val) { var next = key + ": "; next += $.isPlainObject(val) ? printObj(val) : val; arr.push(next); }); return "{ " + arr.join(", ") + " }"; }; $("#log").append(printObj(object1)); var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; var settings: typeof defaults = $.extend({}, defaults, options); } function test_fadeIn() { $('#clickme').click(function () { $('#book').fadeIn('slow', function () { }); }); $(document.body).click(function () { $("div:hidden:first").fadeIn("slow"); }); $("a").click(function () { $("div").fadeIn(3000, function () { $("span").fadeIn(100); }); return false; }); } function test_fadeOut() { $('#clickme').click(function () { $('#book').fadeOut('slow', function () { }); }); $("p").click(function () { $("p").fadeOut("slow"); }); $("span").click(function () { $(this).fadeOut(1000, function () { $("div").text("'" + $(this).text() + "' has faded!"); $(this).remove(); }); }); $("span").hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); }); $("#btn1").click(function () { function complete() { $("<div/>").text(this.id).appendTo("#log"); } $("#box1").fadeOut(1600, "linear", complete); $("#box2").fadeOut(1600, complete); }); $("#btn2").click(function () { $("div").show(); $("#log").empty(); }); } function test_fadeTo() { $('#clickme').click(function () { $('#book').fadeTo('slow', 0.5, function () { }); }); $("p:first").click(function () { $(this).fadeTo("slow", 0.33); }); $("div").click(function () { $(this).fadeTo("fast", Math.random()); }); var getPos = function (n) { return (Math.floor(n) * 90) + "px"; }; $("p").each(function (n) { var r = Math.floor(Math.random() * 3); var tmp = $(this).text(); $(this).text($("p:eq(" + r + ")").text()); $("p:eq(" + r + ")").text(tmp); $(this).css("left", getPos(n)); }); $("div").each(function (n) { $(this).css("left", getPos(n)); }) .css("cursor", "pointer") .click(function () { $(this).fadeTo(250, 0.25, function () { $(this).css("cursor", "") .prev().css({ "font-weight": "bolder", "font-style": "italic" }); }); }); } function test_fadeToggle() { $("button:first").click(function () { $("p:first").fadeToggle("slow", "linear"); }); $("button:last").click(function () { $("p:last").fadeToggle("fast", function () { $("#log").append("<div>finished</div>"); }); }); } function test_filter() { $('li').filter(':even').css('background-color', 'red'); $('li').filter(function (index) { return index % 3 == 2; }).css('background-color', 'red'); $("div").css("background", "#b4b0da") .filter(function (index) { return index == 1 || $(this).attr("id") == "fourth"; }) .css("border", "3px double red"); $("div").filter(document.getElementById("unique")); $("div").filter($("#unique")); } function test_find() { $('li.item-ii').find('li').css('background-color', 'red'); var item1 = $('li.item-1')[0]; $('li.item-ii').find(item1).css('background-color', 'red'); var $spans = $('span'); $("p").find($spans).css('color', 'red'); var newText = $("p").text().split(" ").join("</span> <span>"); newText = "<span>" + newText + "</span>"; $("p").html(newText) .find('span') .hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); }) .end() .find(":contains('t')") .css({ "font-style": "italic", "font-weight": "bolder" }); } function test_finish() { $(".box").finish(); } function test_first() { $('li').first().css('background-color', 'red'); } function test_focus() { $('#target').focus(function () { alert('Handler for .focus() called.'); }); $('#other').click(function () { $('#target').focus(); }); $("input").focus(function () { $(this).next("span").css('display', 'inline').fadeOut(1000); }); $("input[type=text]").focus(function () { $(this).blur(); }); $(document).ready(function () { $("#login").focus(); }); } function test_focusin() { $("p").focusin(function () { $(this).find("span").css('display', 'inline').fadeOut(1000); }); } function test_focusout() { var fo = 0, b = 0; $("p").focusout(function () { fo++; $("#fo") .text("focusout fired: " + fo + "x"); }).blur(function () { b++; $("#b") .text("blur fired: " + b + "x"); }); } function test_fx() { jQuery.fx.interval = 100; $("input").click(function () { $("div").toggle(3000); }); var toggleFx = function () { $.fx.off = !$.fx.off; }; toggleFx(); $("button").click(toggleFx) $("input").click(function () { $("div").toggle("slow"); }); } function test_get() { $.get('ajax/test.html', function (data) { $('.result').html(data); alert('Load was performed.'); }); var jqxhr = $.get("example.php", function () { alert("success"); }) .done(function () { alert("second success"); }) .fail(function () { alert("error"); }); $.get("test.php"); $.get("test.php", { name: "John", time: "2pm" }); $.get("test.php", { 'choices[]': ["Jon", "Susan"] }); $.get("test.php",