PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/GeneralStudy/TypeScriptHTMLApp1/DefinitelyTyped/jquery/jquery-tests.ts

http://mypet.codeplex.com
TypeScript | 2288 lines | 2093 code | 133 blank | 62 comment | 128 complexity | d6d142f209f87e2a3d60524d73fbdbcb MD5 | raw file
Possible License(s): LGPL-3.0, MIT
  1. /// <reference path="jquery-1.8.d.ts" />
  2. function test_add() {
  3. $("p").add("div").addClass("widget");
  4. var pdiv = $("p").add("div");
  5. $('li').add('p').css('background-color', 'red');
  6. $('li').add(document.getElementsByTagName('p')[0])
  7. .css('background-color', 'red');
  8. $('li').add('<p id="new">new paragraph</p>')
  9. .css('background-color', 'red');
  10. $("div").css("border", "2px solid red")
  11. .add("p")
  12. .css("background", "yellow");
  13. $("p").add("span").css("background", "yellow");
  14. $("p").clone().add("<span>Again</span>").appendTo(document.body);
  15. $("p").add(document.getElementById("a")).css("background", "yellow");
  16. var collection = $("p");
  17. collection = collection.add(document.getElementById("a"));
  18. collection.css("background", "yellow");
  19. }
  20. function test_addClass() {
  21. $("p").addClass("myClass yourClass");
  22. $("p").removeClass("myClass noClass").addClass("yourClass");
  23. $("ul li:last").addClass(function (index) {
  24. return "item-" + index;
  25. });
  26. $("p:last").addClass("selected");
  27. $("p:last").addClass("selected highlight");
  28. $("div").addClass(function (index, currentClass) {
  29. var addedClass: string;
  30. if (currentClass === "red") {
  31. addedClass = "green";
  32. $("p").text("There is one green div");
  33. }
  34. return addedClass;
  35. });
  36. }
  37. function test_after() {
  38. $('.inner').after('<p>Test</p>');
  39. $('<div/>').after('<p></p>');
  40. $('<div/>').after('<p></p>').addClass('foo')
  41. .filter('p').attr('id', 'bar').html('hello')
  42. .end()
  43. .appendTo('body');
  44. $('p').after(function () {
  45. return '<div>' + this.className + '</div>';
  46. });
  47. var $newdiv1 = $('<div id="object1"/>'),
  48. newdiv2 = document.createElement('div'),
  49. existingdiv1 = document.getElementById('foo');
  50. $('p').first().after($newdiv1, [newdiv2, existingdiv1]);
  51. $("p").after(document.createTextNode("Hello"));
  52. $("p").after($("b"));
  53. }
  54. function test_ajax() {
  55. $.ajax({
  56. url: "test.html",
  57. context: document.body
  58. }).done(function () {
  59. $(this).addClass("done");
  60. });
  61. $.ajax({
  62. statusCode: {
  63. 404: function () {
  64. alert("page not found");
  65. }
  66. }
  67. });
  68. $.ajax({
  69. url: "http://fiddle.jshell.net/favicon.png",
  70. beforeSend: function (xhr) {
  71. xhr.overrideMimeType("text/plain; charset=x-user-defined");
  72. }
  73. }).done(function (data) {
  74. if (console && console.log) {
  75. console.log("Sample of data:", data.slice(0, 100));
  76. }
  77. });
  78. $.ajax({
  79. url: 'ajax/test.html',
  80. success: function (data) {
  81. $('.result').html(data);
  82. alert('Load was performed.');
  83. }
  84. });
  85. var _super = jQuery.ajaxSettings.xhr;
  86. jQuery.ajaxSettings.xhr = function () {
  87. var xhr = _super(),
  88. getAllResponseHeaders = xhr.getAllResponseHeaders;
  89. xhr.getAllResponseHeaders = function () {
  90. if (getAllResponseHeaders()) {
  91. return getAllResponseHeaders();
  92. }
  93. var allHeaders = "";
  94. $(["Cache-Control", "Content-Language", "Content-Type",
  95. "Expires", "Last-Modified", "Pragma"]).each(function (i, header_name) {
  96. if (xhr.getResponseHeader(header_name)) {
  97. allHeaders += header_name + ": " + xhr.getResponseHeader(header_name) + "\n";
  98. }
  99. return allHeaders;
  100. });
  101. };
  102. return xhr;
  103. };
  104. $.ajax({
  105. type: "POST",
  106. url: "some.php",
  107. data: { name: "John", location: "Boston" }
  108. }).done(function (msg) {
  109. alert("Data Saved: " + msg);
  110. });
  111. $.ajax({
  112. url: "test.html",
  113. cache: false
  114. }).done(function (html) {
  115. $("#results").append(html);
  116. });
  117. var xmlDocument = [];
  118. var xmlRequest = $.ajax({
  119. url: "page.php",
  120. processData: false,
  121. data: xmlDocument
  122. });
  123. var handleResponse;
  124. xmlRequest.done(handleResponse);
  125. var menuId = $("ul.nav").first().attr("id");
  126. var request = $.ajax({
  127. url: "script.php",
  128. type: "POST",
  129. data: { id: menuId },
  130. dataType: "html"
  131. });
  132. request.done(function (msg) {
  133. $("#log").html(msg);
  134. });
  135. request.fail(function (jqXHR, textStatus) {
  136. alert("Request failed: " + textStatus);
  137. });
  138. $.ajax({
  139. type: "GET",
  140. url: "test.js",
  141. dataType: "script"
  142. });
  143. }
  144. function test_ajaxComplete() {
  145. $('.log').ajaxComplete(function () {
  146. $(this).text('Triggered ajaxComplete handler.');
  147. });
  148. $('.trigger').click(function () {
  149. $('.result').load('ajax/test.html');
  150. });
  151. $('.log').ajaxComplete(function (e, xhr, settings) {
  152. if (settings.url == 'ajax/test.html') {
  153. $(this).text('Triggered ajaxComplete handler. The result is ' + xhr.responseHTML);
  154. }
  155. });
  156. $("#msg").ajaxComplete(function (event, request, settings) {
  157. $(this).append("<li>Request Complete.</li>");
  158. });
  159. }
  160. function test_ajaxError() {
  161. $("div.log").ajaxError(function () {
  162. $(this).text("Triggered ajaxError handler.");
  163. });
  164. $("button.trigger").click(function () {
  165. $("div.result").load("ajax/missing.html");
  166. });
  167. $("div.log").ajaxError(function (e, jqxhr, settings, exception) {
  168. if (settings.url == "ajax/missing.html") {
  169. $(this).text("Triggered ajaxError handler.");
  170. }
  171. });
  172. $("#msg").ajaxError(function (event, request, settings) {
  173. $(this).append("<li>Error requesting page " + settings.url + "</li>");
  174. });
  175. }
  176. function test_ajaxPrefilter() {
  177. var currentRequests = {};
  178. $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
  179. if (options.abortOnRetry) {
  180. if (currentRequests[options.url]) {
  181. currentRequests[options.url].abort();
  182. }
  183. currentRequests[options.url] = jqXHR;
  184. }
  185. });
  186. $.ajaxPrefilter(function (options) {
  187. if (options.crossDomain) {
  188. options.url = "http://mydomain.net/proxy/" + encodeURIComponent(options.url);
  189. options.crossDomain = false;
  190. }
  191. });
  192. $.ajaxPrefilter("json script", function (options, originalOptions, jqXHR) {
  193. });
  194. var isActuallyScript;
  195. $.ajaxPrefilter(function (options) {
  196. if (isActuallyScript(options.url)) {
  197. return "script";
  198. }
  199. });
  200. }
  201. function test_ajaxSend() {
  202. $('.log').ajaxSend(function () {
  203. $(this).text('Triggered ajaxSend handler.');
  204. });
  205. $('.trigger').click(function () {
  206. $('.result').load('ajax/test.html');
  207. });
  208. $('.log').ajaxSend(function (e, jqxhr, settings) {
  209. if (settings.url == 'ajax/test.html') {
  210. $(this).text('Triggered ajaxSend handler.');
  211. }
  212. });
  213. $("#msg").ajaxSend(function (evt, request, settings) {
  214. $(this).append("<li>Starting request at " + settings.url + "</li>");
  215. });
  216. }
  217. function test_ajaxSetup() {
  218. $.ajaxSetup({
  219. url: 'ping.php'
  220. });
  221. $.ajax({
  222. data: { 'name': 'Dan' }
  223. });
  224. $.ajaxSetup({
  225. url: "/xmlhttp/",
  226. global: false,
  227. type: "POST"
  228. });
  229. }
  230. function test_ajaxStart() {
  231. $('.log').ajaxStart(function () {
  232. $(this).text('Triggered ajaxStart handler.');
  233. });
  234. $('.trigger').click(function () {
  235. $('.result').load('ajax/test.html');
  236. });
  237. $("#loading").ajaxStart(function () {
  238. $(this).show();
  239. });
  240. }
  241. function test_ajaxStop() {
  242. $('.log').ajaxStop(function () {
  243. $(this).text('Triggered ajaxStop handler.');
  244. });
  245. $('.trigger').click(function () {
  246. $('.result').load('ajax/test.html');
  247. });
  248. $("#loading").ajaxStop(function () {
  249. $(this).hide();
  250. });
  251. }
  252. function test_ajaxSuccess() {
  253. $('.log').ajaxSuccess(function () {
  254. $(this).text('Triggered ajaxSuccess handler.');
  255. });
  256. $('.trigger').click(function () {
  257. $('.result').load('ajax/test.html');
  258. });
  259. $('.log').ajaxSuccess(function (e, xhr, settings) {
  260. if (settings.url == 'ajax/test.html') {
  261. $(this).text('Triggered ajaxSuccess handler. The ajax response was:' + xhr.responseText);
  262. }
  263. });
  264. $("#msg").ajaxSuccess(function (evt, request, settings) {
  265. $(this).append("<li>Successful Request!</li>");
  266. });
  267. }
  268. function test_allSelector() {
  269. var elementCount = $("*").css("border", "3px solid red").length;
  270. $("body").prepend("<h3>" + elementCount + " elements found</h3>");
  271. var elementCount2 = $("#test").find("*").css("border", "3px solid red").length;
  272. $("body").prepend("<h3>" + elementCount2 + " elements found</h3>");
  273. }
  274. function test_andSelf() {
  275. $('li.third-item').nextAll().andSelf()
  276. .css('background-color', 'red');
  277. $("div").find("p").andSelf().addClass("border");
  278. $("div").find("p").addClass("background");
  279. }
  280. function test_animate() {
  281. $('#clickme').click(function () {
  282. $('#book').animate({
  283. opacity: 0.25,
  284. left: '+=50',
  285. height: 'toggle'
  286. }, 5000, function () {
  287. });
  288. });
  289. $('li').animate({
  290. opacity: .5,
  291. height: '50%'
  292. }, {
  293. step: function (now, fx) {
  294. var data = fx.elem.id + ' ' + fx.prop + ': ' + now;
  295. $('body').append('<div>' + data + '</div>');
  296. }
  297. });
  298. $('#clickme').click(function () {
  299. $('#book').animate({
  300. width: ['toggle', 'swing'],
  301. height: ['toggle', 'swing'],
  302. opacity: 'toggle'
  303. }, 5000, 'linear', function () {
  304. $(this).after('<div>Animation complete.</div>');
  305. });
  306. });
  307. $('#clickme').click(function () {
  308. $('#book').animate({
  309. width: 'toggle',
  310. height: 'toggle'
  311. }, {
  312. duration: 5000,
  313. specialEasing: {
  314. width: 'linear',
  315. height: 'easeOutBounce'
  316. },
  317. complete: function () {
  318. $(this).after('<div>Animation complete.</div>');
  319. }
  320. });
  321. });
  322. $("#go").click(function () {
  323. $("#block").animate({
  324. width: "70%",
  325. opacity: 0.4,
  326. marginLeft: "0.6in",
  327. fontSize: "3em",
  328. borderWidth: "10px"
  329. }, 1500);
  330. });
  331. $("#right").click(function () {
  332. $(".block").animate({ "left": "+=50px" }, "slow");
  333. });
  334. $("#left").click(function () {
  335. $(".block").animate({ "left": "-=50px" }, "slow");
  336. });
  337. $("#go1").click(function () {
  338. $("#block1").animate({ width: "90%" }, { queue: false, duration: 3000 })
  339. .animate({ fontSize: "24px" }, 1500)
  340. .animate({ borderRightWidth: "15px" }, 1500);
  341. });
  342. $("#go2").click(function () {
  343. $("#block2").animate({ width: "90%" }, 1000)
  344. .animate({ fontSize: "24px" }, 1000)
  345. .animate({ borderLeftWidth: "15px" }, 1000);
  346. });
  347. $("#go3").click(function () {
  348. $("#go1").add("#go2").click();
  349. });
  350. $("#go4").click(function () {
  351. $("div").css({ width: "", fontSize: "", borderWidth: "" });
  352. });
  353. $("#go").click(function () {
  354. $(".block:first").animate({
  355. left: 100
  356. }, {
  357. duration: 1000,
  358. step: function (now, fx) {
  359. $(".block:gt(0)").css("left", now);
  360. }
  361. });
  362. });
  363. $("p").animate({
  364. height: "toggle", opacity: "toggle"
  365. }, "slow");
  366. $("p").animate({
  367. left: 50, opacity: 1
  368. }, 500);
  369. $("p").animate({
  370. left: "50px", opacity: 1
  371. }, { duration: 500, queue: false });
  372. $("p").animate({
  373. opacity: "show"
  374. }, "slow", "easein");
  375. $("p").animate({
  376. height: "toggle", opacity: "toggle"
  377. }, { duration: "slow" });
  378. $("p").animate({
  379. opacity: "show"
  380. }, { duration: "slow", easing: "easein" });
  381. $("p").animate({
  382. height: 200, width: 400, opacity: 0.5
  383. }, 1000, "linear", function () {
  384. alert("all done");
  385. });
  386. }
  387. function test_animatedSelector() {
  388. $("#run").click(function () {
  389. $("div:animated").toggleClass("colored");
  390. });
  391. function animateIt() {
  392. $("#mover").slideToggle("slow", animateIt);
  393. }
  394. animateIt();
  395. }
  396. function test_append() {
  397. $('.inner').append('<p>Test</p>');
  398. $('.container').append($('h2'));
  399. var $newdiv1 = $('<div id="object1"/>'),
  400. newdiv2 = document.createElement('div'),
  401. existingdiv1 = document.getElementById('foo');
  402. $('body').append($newdiv1, [newdiv2, existingdiv1]);
  403. }
  404. function test_appendTo() {
  405. $('<p>Test</p>').appendTo('.inner');
  406. $('h2').appendTo($('.container'));
  407. }
  408. function test_attr() {
  409. var title = $("em").attr("title");
  410. $("div").text(title);
  411. $('#greatphoto').attr('alt', 'Beijing Brush Seller');
  412. $('#greatphoto')
  413. .attr('title', 'Photo by Kelly Clark');
  414. $('#greatphoto').attr({
  415. alt: 'Beijing Brush Seller',
  416. title: 'photo by Kelly Clark'
  417. });
  418. $('#greatphoto').attr('title', function (i, val) {
  419. return val + ' - photo by Kelly Clark'
  420. });
  421. $("div").attr("id", function (arr) {
  422. return "div-id" + arr;
  423. })
  424. .each(function () {
  425. $("span", this).html("(ID = '<b>" + this.id + "</b>')");
  426. });
  427. $("img").attr("src", function () {
  428. return "/images/" + this.title;
  429. });
  430. }
  431. function test_attributeSelectors() {
  432. $('a[hreflang|="en"]').css('border', '3px dotted green');
  433. $('input[name*="man"]').val('has man in it!');
  434. $('input[name~="man"]').val('mr. man is in it!');
  435. $('input[name$="letter"]').val('a letter');
  436. $('input[value="Hot Fuzz"]').next().text(" Hot Fuzz");
  437. $('input[name!="newsletter"]').next().append('<b>; not newsletter</b>');
  438. $('input[name^="news"]').val('news here!');
  439. }
  440. function test_before() {
  441. $('.inner').before('<p>Test</p>');
  442. $('.container').before($('h2'));
  443. $("<div/>").before("<p></p>");
  444. var $newdiv1 = $('<div id="object1"/>'),
  445. newdiv2 = document.createElement('div'),
  446. existingdiv1 = document.getElementById('foo');
  447. $('p').first().before($newdiv1, [newdiv2, existingdiv1]);
  448. }
  449. function test_bind() {
  450. $('#foo').bind('click', function () {
  451. alert('User clicked on "foo."');
  452. });
  453. $('#foo').bind('mouseenter mouseleave', function () {
  454. $(this).toggleClass('entered');
  455. });
  456. $('#foo').bind({
  457. click: function () { },
  458. mouseenter: function () { }
  459. });
  460. $('#foo').bind('click', function () {
  461. alert($(this).text());
  462. });
  463. $(document).ready(function () {
  464. $('#foo').bind('click', function (event) {
  465. alert('The mouse cursor is at ('
  466. + event.pageX + ', ' + event.pageY + ')');
  467. });
  468. });
  469. var message = 'Spoon!';
  470. $('#foo').bind('click', function () {
  471. alert(message);
  472. });
  473. message = 'Not in the face!';
  474. $('#bar').bind('click', function () {
  475. alert(message);
  476. });
  477. var message = 'Spoon!';
  478. $('#foo').bind('click', { msg: message }, function (event) {
  479. alert(event.data.msg);
  480. });
  481. message = 'Not in the face!';
  482. $('#bar').bind('click', { msg: message }, function (event) {
  483. alert(event.data.msg);
  484. });
  485. $("p").bind("click", function (event) {
  486. var str = "( " + event.pageX + ", " + event.pageY + " )";
  487. $("span").text("Click happened! " + str);
  488. });
  489. $("p").bind("dblclick", function () {
  490. $("span").text("Double-click happened in " + this.nodeName);
  491. });
  492. $("p").bind("mouseenter mouseleave", function (event) {
  493. $(this).toggleClass("over");
  494. });
  495. $("p").bind("click", function () {
  496. alert($(this).text());
  497. });
  498. function handler(event) {
  499. alert(event.data.foo);
  500. }
  501. $("p").bind("click", { foo: "bar" }, handler)
  502. $("form").bind("submit", function () { return false; })
  503. $("form").bind("submit", function (event) {
  504. event.preventDefault();
  505. });
  506. $("form").bind("submit", function (event) {
  507. event.stopPropagation();
  508. });
  509. $("p").bind("myCustomEvent", function (e, myName, myValue) {
  510. $(this).text(myName + ", hi there!");
  511. $("span").stop().css("opacity", 1)
  512. .text("myName = " + myName)
  513. .fadeIn(30).fadeOut(1000);
  514. });
  515. $("button").click(function () {
  516. $("p").trigger("myCustomEvent", ["John"]);
  517. });
  518. $("div.test").bind({
  519. click: function () {
  520. $(this).addClass("active");
  521. },
  522. mouseenter: function () {
  523. $(this).addClass("inside");
  524. },
  525. mouseleave: function () {
  526. $(this).removeClass("inside");
  527. }
  528. });
  529. }
  530. function test_blur() {
  531. $('#target').blur(function () {
  532. alert('Handler for .blur() called.');
  533. });
  534. $('#other').click(function () {
  535. $('#target').blur();
  536. });
  537. $("p").blur();
  538. }
  539. function test_browser() {
  540. jQuery.each(jQuery.browser, function (i, val) {
  541. $("<div>" + i + " : <span>" + val + "</span>").appendTo(document.body);
  542. });
  543. $.browser.msie;
  544. if ($.browser.webkit) {
  545. alert("this is webkit!");
  546. }
  547. var ua = $.browser;
  548. if (ua.mozilla && ua.version.slice(0, 3) == "1.9") {
  549. alert("Do stuff for firefox 3");
  550. }
  551. if ($.browser.msie) {
  552. $("#div ul li").css("display", "inline");
  553. } else {
  554. $("#div ul li").css("display", "inline-table");
  555. }
  556. $("p").html("The version number of the rendering engine your browser uses is: <span>" + $.browser.version + "</span>");
  557. if ($.browser.msie) {
  558. alert($.browser.version);
  559. }
  560. if ($.browser.msie) {
  561. parseInt($.browser.version, 10);
  562. }
  563. }
  564. interface JQueryStatic { Topic; }
  565. function test_callbacks() {
  566. function fn1(value) {
  567. console.log(value);
  568. }
  569. function fn2(value) {
  570. fn1("fn2 says:" + value);
  571. return false;
  572. }
  573. var callbacks = $.Callbacks();
  574. var callbacks2 = $.Callbacks("once");
  575. callbacks.add(fn1);
  576. callbacks.fire("foo!");
  577. callbacks.add(fn2);
  578. callbacks.fire("bar!");
  579. callbacks.remove(fn2);
  580. callbacks.fire("foobar");
  581. var topics = {};
  582. jQuery.Topic = function (id) {
  583. var callbacks,
  584. method,
  585. topic = id && topics[id];
  586. if (!topic) {
  587. callbacks = jQuery.Callbacks();
  588. topic = {
  589. publish: callbacks.fire,
  590. subscribe: callbacks.add,
  591. unsubscribe: callbacks.remove
  592. };
  593. if (id) {
  594. topics[id] = topic;
  595. }
  596. }
  597. return topic;
  598. };
  599. $.Topic("mailArrived").subscribe(fn1);
  600. $.Topic("mailArrived").subscribe(fn2);
  601. $.Topic("mailSent").subscribe(fn1);
  602. $.Topic("mailArrived").publish("hello world!");
  603. $.Topic("mailSent").publish("woo! mail!");
  604. $.Topic("mailArrived").subscribe(fn1);
  605. var dfd = $.Deferred();
  606. var topic = $.Topic("mailArrived");
  607. dfd.done(topic.publish);
  608. dfd.resolve("its been published!");
  609. }
  610. function test_callbacksFunctions() {
  611. var foo = function (value) {
  612. console.log('foo:' + value);
  613. }
  614. var bar = function (value) {
  615. console.log('bar:' + value);
  616. }
  617. var callbacks = $.Callbacks();
  618. callbacks.add(foo);
  619. callbacks.fire('hello');
  620. callbacks.add(bar);
  621. callbacks.fire('world');
  622. callbacks.disable();
  623. callbacks.empty();
  624. callbacks.fire('hello');
  625. console.log(callbacks.fired());
  626. callbacks.fireWith(window, ['foo', 'bar']);
  627. var foo2 = function (value1, value2) {
  628. console.log('Received:' + value1 + ',' + value2);
  629. };
  630. console.log(callbacks.has(foo2));
  631. callbacks.lock();
  632. console.log(callbacks.locked());
  633. callbacks.remove(foo);
  634. }
  635. function test_change() {
  636. $('.target').change(function () {
  637. alert('Handler for .change() called.');
  638. });
  639. $('#other').click(function () {
  640. $('.target').change();
  641. });
  642. $("input[type='text']").change(function () { });
  643. }
  644. function test_children() {
  645. $('ul.level-2').children().css('background-color', 'red');
  646. $("#container").click(function (e) {
  647. $("*").removeClass("hilite");
  648. var $kids = $(e.target).children();
  649. var len = $kids.addClass("hilite").length;
  650. $("#results span:first").text(len.toString());
  651. //$("#results span:last").text(e.target.tagName);
  652. e.preventDefault();
  653. return false;
  654. });
  655. $("div").children(".selected").css("color", "blue");
  656. }
  657. function test_clearQueue() {
  658. $("#start").click(function () {
  659. var myDiv = $("div");
  660. myDiv.show("slow");
  661. myDiv.animate({ left: '+=200' }, 5000);
  662. myDiv.queue(function () {
  663. var _this = $(this);
  664. _this.addClass("newcolor");
  665. _this.dequeue();
  666. });
  667. myDiv.animate({ left: '-=200' }, 1500);
  668. myDiv.queue(function () {
  669. var _this = $(this);
  670. _this.removeClass("newcolor");
  671. _this.dequeue();
  672. });
  673. myDiv.slideUp();
  674. });
  675. $("#stop").click(function () {
  676. var myDiv = $("div");
  677. myDiv.clearQueue();
  678. myDiv.stop();
  679. });
  680. }
  681. function test_click() {
  682. $("#target").click(function () {
  683. alert("Handler for .click() called.");
  684. });
  685. $("#other").click(function () {
  686. $("#target").click();
  687. });
  688. $("p").click(function () {
  689. $(this).slideUp();
  690. });
  691. $("p").click();
  692. }
  693. function test_clone() {
  694. $('.hello').clone().appendTo('.goodbye');
  695. var $elem = $('#elem').data({ "arr": [1] }),
  696. $clone = $elem.clone(true)
  697. .data("arr", $.extend([], $elem.data("arr")));
  698. $("b").clone().prependTo("p");
  699. $('#copy').append($('#orig .elem')
  700. .clone()
  701. .children('a')
  702. .prepend('foo - ')
  703. .parent()
  704. .clone());
  705. }
  706. function test_closest() {
  707. $('li.item-a').closest('ul')
  708. .css('background-color', 'red');
  709. $('li.item-a').closest('li')
  710. .css('background-color', 'red');
  711. var listItemII = document.getElementById('ii');
  712. $('li.item-a').closest('ul', listItemII)
  713. .css('background-color', 'red');
  714. $('li.item-a').closest('#one', listItemII)
  715. .css('background-color', 'green');
  716. $(document).bind("click", function (e) {
  717. $(e.target).closest("li").toggleClass("hilight");
  718. });
  719. var $listElements = $("li").css("color", "blue");
  720. $(document).bind("click", function (e) {
  721. //$(e.target).closest($listElements).toggleClass("hilight");
  722. });
  723. }
  724. function test_contains() {
  725. jQuery.contains(document.documentElement, document.body);
  726. jQuery.contains(document.body, document.documentElement);
  727. }
  728. function test_contents() {
  729. $('.container').contents().filter(function () {
  730. return this.nodeType == 3;
  731. })
  732. .wrap('<p></p>')
  733. .end()
  734. .filter('br')
  735. .remove();
  736. $("#frameDemo").contents().find("a").css("background-color", "#BADA55");
  737. }
  738. function test_context() {
  739. $("ul")
  740. .append("<li>" + $("ul").context + "</li>")
  741. .append("<li>" + $("ul", document.body).context.nodeName + "</li>");
  742. }
  743. function test_css() {
  744. $("div").click(function () {
  745. var color = $(this).css("background-color");
  746. $("#result").html("That div is <span style='color:" + color + ";'>" + color + "</span>.");
  747. });
  748. $('div.example').css('width', function (index) {
  749. return index * 50;
  750. });
  751. $("p").mouseover(function () {
  752. $(this).css("color", "red");
  753. });
  754. $("#box").one("click", function () {
  755. $(this).css("width", "+=200");
  756. });
  757. var words = $("p:first").text().split(" ");
  758. var text = words.join("</span> <span>");
  759. $("p:first").html("<span>" + text + "</span>");
  760. $("span").click(function () {
  761. $(this).css("background-color", "yellow");
  762. });
  763. $("p").hover(function () {
  764. $(this).css({ 'background-color': 'yellow', 'font-weight': 'bolder' });
  765. }, function () {
  766. var cssObj = {
  767. 'background-color': '#ddd',
  768. 'font-weight': '',
  769. 'color': 'rgb(0,40,244)'
  770. }
  771. $(this).css(cssObj);
  772. });
  773. $("div").click(function () {
  774. $(this).css({
  775. width: function (index, value) {
  776. return parseFloat(value) * 1.2;
  777. },
  778. height: function (index, value) {
  779. return parseFloat(value) * 1.2;
  780. }
  781. });
  782. });
  783. }
  784. function test_cssHooks() {
  785. if (!$.cssHooks) {
  786. throw ("jQuery 1.4.3 or above is required for this plugin to work");
  787. return;
  788. }
  789. $.cssHooks["someCSSProp"] = {
  790. get: function (elem, computed, extra) { },
  791. set: function (elem, value) { }
  792. };
  793. function styleSupport(prop) {
  794. var vendorProp, supportedProp,
  795. capProp = prop.charAt(0).toUpperCase() + prop.slice(1),
  796. prefixes = ["Moz", "Webkit", "O", "ms"],
  797. div = document.createElement("div");
  798. if (prop in div.style) {
  799. supportedProp = prop;
  800. } else {
  801. for (var i = 0; i < prefixes.length; i++) {
  802. vendorProp = prefixes[i] + capProp;
  803. if (vendorProp in div.style) {
  804. supportedProp = vendorProp;
  805. break;
  806. }
  807. }
  808. }
  809. div = null;
  810. $.support[prop] = supportedProp;
  811. return supportedProp;
  812. }
  813. styleSupport("borderRadius");
  814. $.cssNumber["someCSSProp"] = true;
  815. $.fx.step["someCSSProp"] = function (fx) {
  816. $.cssHooks["someCSSProp"].set(fx.elem, fx.now + fx.unit);
  817. };
  818. }
  819. function test_data() {
  820. $('body').data('foo', 52);
  821. $('body').data('bar', { myType: 'test', count: 40 });
  822. $('body').data('foo');
  823. $('body').data();
  824. $("div").data("test", { first: 16, last: "pizza!" });
  825. $("span:first").text($("div").data("test").first);
  826. $("span:last").text($("div").data("test").last);
  827. alert($('body').data('foo'));
  828. alert($('body').data());
  829. alert($("body").data("foo"));
  830. $("body").data("bar", "foobar");
  831. alert($("body").data("bar"));
  832. $("div").data("role") === "page";
  833. $("div").data("lastValue") === 43;
  834. $("div").data("hidden") === true;
  835. $("div").data("options").name === "John";
  836. var value;
  837. switch ($("button").index(this)) {
  838. case 0:
  839. value = $("div").data("blah");
  840. break;
  841. case 1:
  842. $("div").data("blah", "hello");
  843. value = "Stored!";
  844. break;
  845. case 2:
  846. $("div").data("blah", 86);
  847. value = "Stored!";
  848. break;
  849. case 3:
  850. $("div").removeData("blah");
  851. value = "Removed!";
  852. break;
  853. }
  854. $("span").text("" + value);
  855. jQuery.data(document.body, 'foo', 52);
  856. jQuery.data(document.body, 'bar', 'test');
  857. var div = $("div")[0];
  858. jQuery.data(div, "test", { first: 16, last: "pizza!" });
  859. $("span:first").text(jQuery.data(div, "test").first);
  860. $("span:last").text(jQuery.data(div, "test").last);
  861. }
  862. function test_dblclick() {
  863. $('#target').dblclick(function () {
  864. alert('Handler for .dblclick() called.');
  865. });
  866. $('#other').click(function () {
  867. $('#target').dblclick();
  868. });
  869. $("p").dblclick(function () { alert("Hello World!"); });
  870. var divdbl = $("div:first");
  871. divdbl.dblclick(function () {
  872. divdbl.toggleClass('dbl');
  873. });
  874. }
  875. function test_deferred() {
  876. $.get("test.php").always(function () {
  877. alert("$.get completed with success or error callback arguments");
  878. });
  879. $.get("test.php").done(function () {
  880. alert("$.get succeeded");
  881. });
  882. function fn1() {
  883. $("p").append(" 1 ");
  884. }
  885. function fn2() {
  886. $("p").append(" 2 ");
  887. }
  888. function fn3(n) {
  889. $("p").append(n + " 3 " + n);
  890. }
  891. var dfd = $.Deferred();
  892. dfd
  893. .done([fn1, fn2], fn3, [fn2, fn1])
  894. .done(function (n) {
  895. $("p").append(n + " we're done.");
  896. });
  897. $("button").bind("click", function () {
  898. dfd.resolve("and");
  899. });
  900. $.get("test.php")
  901. .done(function () { alert("$.get succeeded"); })
  902. .fail(function () { alert("$.get failed!"); });
  903. dfd.state();
  904. var defer = $.Deferred(),
  905. filtered = defer.pipe(function (value) {
  906. return value * 2;
  907. });
  908. defer.resolve(5);
  909. filtered.done(function (value) {
  910. alert("Value is ( 2*5 = ) 10: " + value);
  911. });
  912. filtered.fail(function (value) {
  913. alert("Value is ( 3*6 = ) 18: " + value);
  914. });
  915. filtered.done(function (data) { });
  916. function asyncEvent() {
  917. var newDeferred = new jQuery.Deferred();
  918. var dfd: JQueryDeferred;
  919. setTimeout(function () {
  920. dfd.resolve("hurray");
  921. }, Math.floor(400 + Math.random() * 2000));
  922. setTimeout(function () {
  923. dfd.reject("sorry");
  924. }, Math.floor(400 + Math.random() * 2000));
  925. setTimeout(function working() {
  926. if (dfd.state() === "pending") {
  927. dfd.notify("working... ");
  928. setTimeout(null, 500);
  929. }
  930. }, 1);
  931. return dfd.promise();
  932. }
  933. var obj = {
  934. hello: function (name) {
  935. alert("Hello " + name);
  936. }
  937. },
  938. defer = $.Deferred();
  939. defer.promise(obj);
  940. defer.resolve("John");
  941. $.get("test.php").then(
  942. function () { alert("$.get succeeded"); },
  943. function () { alert("$.get failed!"); }
  944. );
  945. }
  946. function test_delay() {
  947. $('#foo').slideUp(300).delay(800).fadeIn(400);
  948. $("button").click(function () {
  949. $("div.first").slideUp(300).delay(800).fadeIn(400);
  950. $("div.second").slideUp(300).fadeIn(400);
  951. });
  952. }
  953. /* Not existing, but not recommended either
  954. function test_delegate() {
  955. $("table").delegate("td", "click", function () {
  956. $(this).toggleClass("chosen");
  957. });
  958. $("table").on("click", "td", function () {
  959. $(this).toggleClass("chosen");
  960. });
  961. $("body").delegate("p", "click", function () {
  962. $(this).after("<p>Another paragraph!</p>");
  963. });
  964. $("body").delegate("p", "click", function () {
  965. alert($(this).text());
  966. });
  967. $("body").delegate("a", "click", function () { return false; });
  968. $("body").delegate("a", "click", function (event) {
  969. event.preventDefault();
  970. });
  971. $("body").delegate("p", "myCustomEvent", function (e, myName, myValue) {
  972. $(this).text("Hi there!");
  973. $("span").stop().css("opacity", 1)
  974. .text("myName = " + myName)
  975. .fadeIn(30).fadeOut(1000);
  976. });
  977. $("button").click(function () {
  978. $("p").trigger("myCustomEvent");
  979. });
  980. }
  981. */
  982. function test_dequeue() {
  983. $("button").click(function () {
  984. $("div").animate({ left: '+=200px' }, 2000);
  985. $("div").animate({ top: '0px' }, 600);
  986. $("div").queue(function () {
  987. $(this).toggleClass("red");
  988. $(this).dequeue();
  989. });
  990. $("div").animate({ left: '10px', top: '30px' }, 700);
  991. });
  992. }
  993. function test_detach() {
  994. $("p").click(function () {
  995. $(this).toggleClass("off");
  996. });
  997. var p;
  998. $("button").click(function () {
  999. if (p) {
  1000. p.appendTo("body");
  1001. p = null;
  1002. } else {
  1003. p = $("p").detach();
  1004. }
  1005. });
  1006. }
  1007. /* Not existing, but not recommended either
  1008. function test_die() {
  1009. function aClick() {
  1010. $("div").show().fadeOut("slow");
  1011. }
  1012. $("#bind").click(function () {
  1013. $("#theone").live("click", aClick)
  1014. .text("Can Click!");
  1015. });
  1016. $("#unbind").click(function () {
  1017. $("#theone").die("click", aClick)
  1018. .text("Does nothing...");
  1019. });
  1020. $("p").die();
  1021. $("p").die("click");
  1022. var foo = function () { };
  1023. $("p").live("click", foo);
  1024. $("p").die("click", foo);
  1025. }
  1026. */
  1027. function test_each() {
  1028. $.each([52, 97], function (index, value) {
  1029. alert(index + ': ' + value);
  1030. });
  1031. var map = {
  1032. 'flammable': 'inflammable',
  1033. 'duh': 'no duh'
  1034. };
  1035. $.each(map, function (key, value) {
  1036. alert(key + ': ' + value);
  1037. });
  1038. var arr = ["one", "two", "three", "four", "five"];
  1039. var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
  1040. jQuery.each(arr, function () {
  1041. $("#" + this).text("Mine is " + this + ".");
  1042. return (this != "three");
  1043. });
  1044. jQuery.each(obj, function (i, val) {
  1045. $("#" + i).append(document.createTextNode(" - " + val));
  1046. });
  1047. $.each(['a', 'b', 'c'], function (i, l) {
  1048. alert("Index #" + i + ": " + l);
  1049. });
  1050. $.each({ name: "John", lang: "JS" }, function (k, v) {
  1051. alert("Key: " + k + ", Value: " + v);
  1052. });
  1053. $('li').each(function (index) {
  1054. alert(index + ': ' + $(this).text());
  1055. });
  1056. $(document.body).click(function () {
  1057. $("div").each(function (i) {
  1058. if (this.style.color != "blue") {
  1059. this.style.color = "blue";
  1060. } else {
  1061. this.style.color = "";
  1062. }
  1063. });
  1064. });
  1065. $("span").click(function () {
  1066. $("li").each(function () {
  1067. $(this).toggleClass("example");
  1068. });
  1069. });
  1070. $("button").click(function () {
  1071. $("div").each(function (index, domEle) {
  1072. // domEle == this
  1073. $(domEle).css("backgroundColor", "yellow");
  1074. if ($(this).is("#stop")) {
  1075. $("span").text("Stopped at div index #" + index);
  1076. return false;
  1077. }
  1078. });
  1079. });
  1080. }
  1081. function test_empty() {
  1082. $('.hello').empty();
  1083. }
  1084. function test_end() {
  1085. $('ul.first').find('.foo').css('background-color', 'red')
  1086. .end().find('.bar').css('background-color', 'green');
  1087. $('ul.first').find('.foo')
  1088. .css('background-color', 'red')
  1089. .end().find('.bar')
  1090. .css('background-color', 'green')
  1091. .end();
  1092. }
  1093. function test_eq() {
  1094. $('li').eq(2).css('background-color', 'red');
  1095. $('li').eq(-2).css('background-color', 'red');
  1096. $('li').eq(5).css('background-color', 'red');
  1097. $("body").find("div").eq(2).addClass("blue");
  1098. }
  1099. function test_error() {
  1100. $('#book')
  1101. .error(function () {
  1102. alert('Handler for .error() called.')
  1103. })
  1104. .attr("src", "missing.png");
  1105. $("img")
  1106. .error(function () {
  1107. $(this).hide();
  1108. })
  1109. .attr("src", "missing.png");
  1110. jQuery.error = console.error;
  1111. }
  1112. function test_eventParams() {
  1113. $("p").click(function (event) {
  1114. event.currentTarget === this;
  1115. });
  1116. $(".box").on("click", "button", function (event) {
  1117. $(event.delegateTarget).css("background-color", "red");
  1118. });
  1119. $("a").click(function (event) {
  1120. event.isDefaultPrevented();
  1121. event.preventDefault();
  1122. event.isDefaultPrevented();
  1123. });
  1124. function immediatePropStopped(e) {
  1125. var msg = "";
  1126. if (e.isImmediatePropagationStopped()) {
  1127. msg = "called"
  1128. } else {
  1129. msg = "not called";
  1130. }
  1131. $("#stop-log").append("<div>" + msg + "</div>");
  1132. }
  1133. $("button").click(function (event) {
  1134. immediatePropStopped(event);
  1135. event.stopImmediatePropagation();
  1136. immediatePropStopped(event);
  1137. });
  1138. function propStopped(e) {
  1139. var msg = "";
  1140. if (e.isPropagationStopped()) {
  1141. msg = "called"
  1142. } else {
  1143. msg = "not called";
  1144. }
  1145. $("#stop-log").append("<div>" + msg + "</div>");
  1146. }
  1147. $("button").click(function (event) {
  1148. propStopped(event);
  1149. event.stopPropagation();
  1150. propStopped(event);
  1151. });
  1152. $("p").bind("test.something", function (event) {
  1153. alert(event.namespace);
  1154. });
  1155. $("button").click(function (event) {
  1156. $("p").trigger("test.something");
  1157. });
  1158. $(document).bind('mousemove', function (e) {
  1159. $("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY);
  1160. });
  1161. $("a").click(function (event) {
  1162. event.preventDefault();
  1163. $('<div/>')
  1164. .append('default ' + event.type + ' prevented')
  1165. .appendTo('#log');
  1166. });
  1167. $("a").mouseout(function (event) {
  1168. alert(event.relatedTarget.nodeName);
  1169. });
  1170. $("button").click(function (event) {
  1171. return "hey";
  1172. });
  1173. $("button").click(function (event) {
  1174. $("p").html(event.result);
  1175. });
  1176. $("p").click(function (event) {
  1177. event.stopImmediatePropagation();
  1178. });
  1179. $("p").click(function (event) {
  1180. $(this).css("background-color", "#f00");
  1181. });
  1182. $("div").click(function (event) {
  1183. $(this).css("background-color", "#f00");
  1184. });
  1185. $("p").click(function (event) {
  1186. event.stopPropagation();
  1187. });
  1188. $("body").click(function (event) {
  1189. $("#log").html("clicked: " + event.target.nodeName);
  1190. });
  1191. $('#whichkey').bind('keydown', function (e) {
  1192. $('#log').html(e.type + ': ' + e.which);
  1193. });
  1194. $('#whichkey').bind('mousedown', function (e) {
  1195. $('#log').html(e.type + ': ' + e.which);
  1196. });
  1197. }
  1198. function test_extend() {
  1199. var object1 = {
  1200. apple: 0,
  1201. banana: { weight: 52, price: 100 },
  1202. cherry: 97
  1203. };
  1204. var object2 = {
  1205. banana: { price: 200 },
  1206. durian: 100
  1207. };
  1208. $.extend(object1, object2);
  1209. var printObj = typeof JSON != "undefined" ? JSON.stringify : function (obj) {
  1210. var arr = [];
  1211. $.each(obj, function (key, val) {
  1212. var next = key + ": ";
  1213. next += $.isPlainObject(val) ? printObj(val) : val;
  1214. arr.push(next);
  1215. });
  1216. return "{ " + arr.join(", ") + " }";
  1217. };
  1218. $("#log").append(printObj(object1));
  1219. var defaults = { validate: false, limit: 5, name: "foo" };
  1220. var options = { validate: true, name: "bar" };
  1221. var settings = $.extend({}, defaults, options);
  1222. }
  1223. function test_fadeIn() {
  1224. $('#clickme').click(function () {
  1225. $('#book').fadeIn('slow', function () { });
  1226. });
  1227. $(document.body).click(function () {
  1228. $("div:hidden:first").fadeIn("slow");
  1229. });
  1230. $("a").click(function () {
  1231. $("div").fadeIn(3000, function () {
  1232. $("span").fadeIn(100);
  1233. });
  1234. return false;
  1235. });
  1236. }
  1237. function test_fadeOut() {
  1238. $('#clickme').click(function () {
  1239. $('#book').fadeOut('slow', function () { });
  1240. });
  1241. $("p").click(function () {
  1242. $("p").fadeOut("slow");
  1243. });
  1244. $("span").click(function () {
  1245. $(this).fadeOut(1000, function () {
  1246. $("div").text("'" + $(this).text() + "' has faded!");
  1247. $(this).remove();
  1248. });
  1249. });
  1250. $("span").hover(function () {
  1251. $(this).addClass("hilite");
  1252. }, function () {
  1253. $(this).removeClass("hilite");
  1254. });
  1255. $("#btn1").click(function () {
  1256. function complete() {
  1257. $("<div/>").text(this.id).appendTo("#log");
  1258. }
  1259. $("#box1").fadeOut(1600, "linear", complete);
  1260. $("#box2").fadeOut(1600, complete);
  1261. });
  1262. $("#btn2").click(function () {
  1263. $("div").show();
  1264. $("#log").empty();
  1265. });
  1266. }
  1267. function test_fadeTo() {
  1268. $('#clickme').click(function () {
  1269. $('#book').fadeTo('slow', 0.5, function () { });
  1270. });
  1271. $("p:first").click(function () {
  1272. $(this).fadeTo("slow", 0.33);
  1273. });
  1274. $("div").click(function () {
  1275. $(this).fadeTo("fast", Math.random());
  1276. });
  1277. var getPos = function (n) {
  1278. return (Math.floor(n) * 90) + "px";
  1279. };
  1280. $("p").each(function (n) {
  1281. var r = Math.floor(Math.random() * 3);
  1282. var tmp = $(this).text();
  1283. $(this).text($("p:eq(" + r + ")").text());
  1284. $("p:eq(" + r + ")").text(tmp);
  1285. $(this).css("left", getPos(n));
  1286. });
  1287. $("div").each(function (n) {
  1288. $(this).css("left", getPos(n));
  1289. })
  1290. .css("cursor", "pointer")
  1291. .click(function () {
  1292. $(this).fadeTo(250, 0.25, function () {
  1293. $(this).css("cursor", "")
  1294. .prev().css({
  1295. "font-weight": "bolder",
  1296. "font-style": "italic"
  1297. });
  1298. });
  1299. });
  1300. }
  1301. function test_fadeToggle() {
  1302. $("button:first").click(function () {
  1303. $("p:first").fadeToggle("slow", "linear");
  1304. });
  1305. $("button:last").click(function () {
  1306. $("p:last").fadeToggle("fast", function () {
  1307. $("#log").append("<div>finished</div>");
  1308. });
  1309. });
  1310. }
  1311. function test_filter() {
  1312. $('li').filter(':even').css('background-color', 'red');
  1313. $('li').filter(function (index) {
  1314. return index % 3 == 2;
  1315. }).css('background-color', 'red');
  1316. $("div").css("background", "#b4b0da")
  1317. .filter(function (index) {
  1318. return index == 1 || $(this).attr("id") == "fourth";
  1319. })
  1320. .css("border", "3px double red");
  1321. $("div").filter(document.getElementById("unique"));
  1322. $("div").filter($("#unique"));
  1323. }
  1324. function test_find() {
  1325. $('li.item-ii').find('li').css('background-color', 'red');
  1326. var item1 = $('li.item-1')[0];
  1327. $('li.item-ii').find(item1).css('background-color', 'red');
  1328. var $spans = $('span');
  1329. $("p").find($spans).css('color', 'red');
  1330. var newText = $("p").text().split(" ").join("</span> <span>");
  1331. newText = "<span>" + newText + "</span>";
  1332. $("p").html(newText)
  1333. .find('span')
  1334. .hover(function () {
  1335. $(this).addClass("hilite");
  1336. },
  1337. function () {
  1338. $(this).removeClass("hilite");
  1339. })
  1340. .end()
  1341. .find(":contains('t')")
  1342. .css({ "font-style": "italic", "font-weight": "bolder" });
  1343. }
  1344. function test_first() {
  1345. $('li').first().css('background-color', 'red');
  1346. }
  1347. function test_focus() {
  1348. $('#target').focus(function () {
  1349. alert('Handler for .focus() called.');
  1350. });
  1351. $('#other').click(function () {
  1352. $('#target').focus();
  1353. });
  1354. $("input").focus(function () {
  1355. $(this).next("span").css('display', 'inline').fadeOut(1000);
  1356. });
  1357. $("input[type=text]").focus(function () {
  1358. $(this).blur();
  1359. });
  1360. $(document).ready(function () {
  1361. $("#login").focus();
  1362. });
  1363. }
  1364. function test_focusin() {
  1365. $("p").focusin(function () {
  1366. $(this).find("span").css('display', 'inline').fadeOut(1000);
  1367. });
  1368. }
  1369. function test_focusout() {
  1370. var fo = 0, b = 0;
  1371. $("p").focusout(function () {
  1372. fo++;
  1373. $("#fo")
  1374. .text("focusout fired: " + fo + "x");
  1375. }).blur(function () {
  1376. b++;
  1377. $("#b")
  1378. .text("blur fired: " + b + "x");
  1379. });
  1380. }
  1381. function test_fx() {
  1382. jQuery.fx.interval = 100;
  1383. $("input").click(function () {
  1384. $("div").toggle(3000);
  1385. });
  1386. var toggleFx = function () {
  1387. $.fx.off = !$.fx.off;
  1388. };
  1389. toggleFx();
  1390. $("button").click(toggleFx)
  1391. $("input").click(function () {
  1392. $("div").toggle("slow");
  1393. });
  1394. }
  1395. function test_get() {
  1396. $.get('ajax/test.html', function (data) {
  1397. $('.result').html(data);
  1398. alert('Load was performed.');
  1399. });
  1400. var jqxhr = $.get("example.php", function () {
  1401. alert("success");
  1402. })
  1403. .done(function () { alert("second success"); })
  1404. .fail(function () { alert("error"); });
  1405. $.get("test.php");
  1406. $.get("test.php", { name: "John", time: "2pm" });
  1407. $.get("test.php", { 'choices[]': ["Jon", "Susan"] });
  1408. $.get("test.php", function (data) {
  1409. alert("Data Loaded: " + data);
  1410. });
  1411. $.get("test.cgi", { name: "John", time: "2pm" },
  1412. function (data) {
  1413. alert("Data Loaded: " + data);
  1414. });
  1415. $.get("test.php",
  1416. function (data) {
  1417. $('body').append("Name: " + data.name)
  1418. .append("Time: " + data.time);
  1419. }, "json");
  1420. alert($('li').get());
  1421. $('li').get(0);
  1422. $('li')[0];
  1423. alert($('li').get(-1));
  1424. function disp(divs) {
  1425. var a = [];
  1426. for (var i = 0; i < divs.length; i++) {
  1427. a.push(divs[i].innerHTML);
  1428. }
  1429. $("span").text(a.join(" "));
  1430. }
  1431. disp($("div").get().reverse());
  1432. $("*", document.body).click(function (e) {
  1433. e.stopPropagation();
  1434. var domEl = $(this).get(0);
  1435. $("span:first").text("Clicked on - " + domEl.tagName);
  1436. });
  1437. }
  1438. function test_getJSON() {
  1439. $.getJSON('ajax/test.json', function (data) {
  1440. var items = [];
  1441. $.each(data, function (key, val) {
  1442. items.push('<li id="' + key + '">' + val + '</li>');
  1443. });
  1444. $('<ul/>', {
  1445. 'class': 'my-new-list',
  1446. html: items.join('')
  1447. }).appendTo('body');
  1448. });
  1449. var jqxhr = $.getJSON("example.json", function () {
  1450. alert("success");
  1451. })
  1452. .done(function () { alert("second success"); })
  1453. .fail(function () { alert("error"); });
  1454. $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  1455. {
  1456. tags: "mount rainier",
  1457. tagmode: "any",
  1458. format: "json"
  1459. },
  1460. function (data) {
  1461. $.each(data.items, function (i, item) {
  1462. $("<img/>").attr("src", item.media.m).appendTo("#images");
  1463. if (i == 3) return false;
  1464. });
  1465. });
  1466. $.getJSON("test.js", function (json) {
  1467. alert("JSON Data: " + json.users[3].name);
  1468. });
  1469. $.getJSON("test.js", { name: "John", time: "2pm" }, function (json) {
  1470. alert("JSON Data: " + json.users[3].name);
  1471. });
  1472. }
  1473. function test_getScript() {
  1474. $.getScript("ajax/test.js", function (data, textStatus, jqxhr) {
  1475. console.log(data);
  1476. console.log(textStatus);
  1477. console.log(jqxhr.status);
  1478. console.log('Load was performed.');
  1479. });
  1480. $.getScript("ajax/test.js")
  1481. .done(function (script, textStatus) {
  1482. console.log(textStatus);
  1483. })
  1484. .fail(function (jqxhr, settings, exception) {
  1485. $("div.log").text("Triggered ajaxError handler.");
  1486. });
  1487. $("div.log").ajaxError(function (e, jqxhr, settings, exception) {
  1488. if (settings.dataType == 'script') {
  1489. $(this).text("Triggered ajaxError handler.");
  1490. }
  1491. });
  1492. $.ajaxSetup({
  1493. cache: true
  1494. });
  1495. $.getScript("/scripts/jquery.color.js", function () {
  1496. $("#go").click(function () {
  1497. $(".block").animate({ backgroundColor: "pink" }, 1000)
  1498. .delay(500)
  1499. .animate({ backgroundColor: "blue" }, 1000);
  1500. });
  1501. });
  1502. }
  1503. function test_globalEval() {
  1504. jQuery.globalEval("var newVar = true;");
  1505. }
  1506. function test_grep() {
  1507. var arr = [1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1];
  1508. $("div").text(arr.join(", "));
  1509. arr = jQuery.grep(arr, function (n, i) {
  1510. return (n != 5 && i > 4);
  1511. });
  1512. $("p").text(arr.join(", "));
  1513. arr = jQuery.grep(arr, function (a) { return a != 9; });
  1514. $("span").text(arr.join(", "));
  1515. $.grep([0, 1, 2], function (n, i) {
  1516. return n > 0;
  1517. }, true);
  1518. }
  1519. function test_has() {
  1520. $('li').has('ul').css('background-color', 'red');
  1521. $("ul").append("<li>" + ($("ul").has("li").length ? "Yes" : "No") + "</li>");
  1522. $("ul").has("li").addClass("full");
  1523. }
  1524. function test_hasClass() {
  1525. $('#mydiv').hasClass('foo');
  1526. $("div#result1").append($("p:first").hasClass("selected").toString());
  1527. $("div#result2").append($("p:last").hasClass("selected").toString());
  1528. $("div#result3").append($("p").hasClass("selected").toString());
  1529. }
  1530. function test_hasData() {
  1531. var $p = jQuery("p"), p = $p[0];
  1532. $p.append(jQuery.hasData(p) + " ");
  1533. $.data(p, "testing", 123);
  1534. $p.append(jQuery.hasData(p) + " ");
  1535. $.removeData(p, "testing");
  1536. $p.append(jQuery.hasData(p) + " ");
  1537. $p.on('click', function () { });
  1538. $p.append(jQuery.hasData(p) + " ");
  1539. $p.off('click');
  1540. $p.append(jQuery.hasData(p) + " ");
  1541. }
  1542. function test_height() {
  1543. $(window).height();
  1544. $(document).height();
  1545. function showHeight(ele, h) {
  1546. $("div").text("The height for the " + ele + " is " + h + "px.");
  1547. }
  1548. $("#getp").click(function () {
  1549. showHeight("paragraph", $("p").height());
  1550. });
  1551. $("#getd").click(function () {
  1552. showHeight("document", $(document).height());
  1553. });
  1554. $("#getw").click(function () {
  1555. showHeight("window", $(window).height());
  1556. });
  1557. $("div").one('click', function () {
  1558. $(this).height(30)
  1559. .css({ cursor: "auto", backgroundColor: "green" });
  1560. });
  1561. }
  1562. function test_hide() {
  1563. $('.target').hide();
  1564. $('#clickme').click(function () {
  1565. $('#book').hide('slow', function () {
  1566. alert('Animation complete.');
  1567. });
  1568. });
  1569. $("p").hide();
  1570. $("a").click(function (event) {
  1571. event.preventDefault();
  1572. $(this).hide();
  1573. });
  1574. $("button").click(function () {
  1575. $("p").hide("slow");
  1576. });
  1577. $("#hidr").click(function () {
  1578. $("span:last-child").hide("fast", function () {
  1579. $(this).prev().hide("fast", arguments.callee);
  1580. });
  1581. });
  1582. $("#showr").click(function () {
  1583. $("span").show(2000);
  1584. });
  1585. $("div").click(function () {
  1586. $(this).hide(2000, function () {
  1587. $(this).remove();
  1588. });
  1589. });
  1590. }
  1591. function test_holdReady() {
  1592. $.holdReady(true);
  1593. $.getScript("myplugin.js", function () {
  1594. $.holdReady(false);
  1595. });
  1596. }
  1597. function test_hover() {
  1598. $("li").hover(
  1599. function () {
  1600. $(this).append($("<span> ***</span>"));
  1601. },
  1602. function () {
  1603. $(this).find("span:last").remove();
  1604. }
  1605. );
  1606. $("li.fade").hover(function () { $(this).fadeOut(100); $(this).fadeIn(500); });
  1607. $("li")
  1608. .filter(":odd")
  1609. .hide()
  1610. .end()
  1611. .filter(":even")
  1612. .hover(
  1613. function () {
  1614. $(this).toggleClass("active")
  1615. .next().stop(true, true).slideToggle();
  1616. }
  1617. );
  1618. }
  1619. function test_html() {
  1620. $('div.demo-container').html();
  1621. $("p").click(function () {
  1622. var htmlStr = $(this).html();
  1623. $(this).text(htmlStr);
  1624. });
  1625. $('div.demo-container')
  1626. .html('<p>All new content. <em>You bet!</em></p>');
  1627. $('div.demo-container').html(function () {
  1628. var emph = '<em>' + $('p').length + ' paragraphs!</em>';
  1629. return '<p>All new content for ' + emph + '</p>';
  1630. });
  1631. $("div").html("<b>Wow!</b> Such excitement...");
  1632. $("div b").append(document.createTextNode("!!!"))
  1633. .css("color", "red");
  1634. }
  1635. function test_inArray() {
  1636. var arr: any[] = [4, "Pete", 8, "John"];
  1637. var $spans = $("span");
  1638. $spans.eq(0).text(jQuery.inArray("John", arr));
  1639. $spans.eq(1).text(jQuery.inArray(4, arr));
  1640. $spans.eq(2).text(jQuery.inArray("Karl", arr));
  1641. $spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
  1642. }
  1643. function test_index() {
  1644. var listItem = document.getElementById('bar');
  1645. alert('Index: ' + $('li').index(listItem));
  1646. var listItems = $('li:gt(0)');
  1647. alert('Index: ' + $('li').index(listItems));
  1648. alert('Index: ' + $('#bar').index());
  1649. $("div").click(function () {
  1650. var index = $("div").index(this);
  1651. $("span").text("That was div index #" + index);
  1652. });
  1653. var listItems = $('li:gt(0)');
  1654. $('div').html('Index: ' + $('li').index(listItems));
  1655. $('div').html('Index: ' + $('#bar').index('li'));
  1656. var foobar = $("li").index($('#foobar'));
  1657. $('div').html('Index: ' + foobar);
  1658. }
  1659. function test_innedHeight() {
  1660. var p = $("p:first");
  1661. $("p:last").text("innerHeight:" + p.innerHeight());
  1662. }
  1663. function test_innerWidth() {
  1664. var p = $("p:first");
  1665. $("p:last").text("innerWidth:" + p.innerWidth());
  1666. }
  1667. function test_insertAfter() {
  1668. $('<p>Test</p>').insertAfter('.inner');
  1669. $('h2').insertAfter($('.container'));
  1670. $("p").insertAfter("#foo");
  1671. }
  1672. function test_insertBefore() {
  1673. $('<p>Test</p>').insertBefore('.inner');
  1674. $('h2').insertBefore($('.container'));
  1675. $("p").insertBefore("#foo");
  1676. }
  1677. function test_is() {
  1678. $("ul").click(function (event) {
  1679. var $target = $(event.target);
  1680. if ($target.is("li")) {
  1681. $target.css("background-color", "red");
  1682. }
  1683. });
  1684. $("li").click(function () {
  1685. var $li = $(this),
  1686. isWithTwo = $li.is(function () {
  1687. return $('strong', this).length === 2;
  1688. });
  1689. if (isWithTwo) {
  1690. $li.css("background-color", "green");
  1691. } else {
  1692. $li.css("background-color", "red");
  1693. }
  1694. });
  1695. $("div").one('click', function () {
  1696. if ($(this).is(":first-child")) {
  1697. $("p").text("It's the first div.");
  1698. } else if ($(this).is(".blue,.red")) {
  1699. $("p").text("It's a blue or red div.");
  1700. } else if ($(this).is(":contains('Peter')")) {
  1701. $("p").text("It's Peter!");
  1702. } else {
  1703. $("p").html("It's nothing <em>special</em>.");
  1704. }
  1705. $("p").hide().slideDown("slow");
  1706. $(this).css({ "border-style": "inset", cursor: "default" });
  1707. });
  1708. var isFormParent = $("input[type='checkbox']").parent().is("form");
  1709. $("div").text("isFormParent = " + isFormParent);
  1710. var isFormParent = $("input[type='checkbox']").parent().is("form");
  1711. $("div").text("isFormParent = " + isFormParent);
  1712. var $alt = $("#browsers li:nth-child(2n)").css("background", "#00FFFF");
  1713. $('li').click(function () {
  1714. var $li = $(this);
  1715. if ($li.is($alt)) {
  1716. $li.slideUp();
  1717. } else {
  1718. $li.css("background", "red");
  1719. }
  1720. });
  1721. var $alt = $("#browsers li:nth-child(2n)").css("background", "#00FFFF");
  1722. $('li').click(function () {
  1723. if ($alt.is(this)) {
  1724. $(this).slideUp();
  1725. } else {
  1726. $(this).css("background", "red");
  1727. }
  1728. });
  1729. }
  1730. function test_isArray() {
  1731. $("b").append("" + $.isArray([]));
  1732. }
  1733. function test_isEmptyObject() {
  1734. jQuery.isEmptyObject({});
  1735. jQuery.isEmptyObject({ foo: "bar" });
  1736. }
  1737. function test_isFuction() {
  1738. function stub() { };
  1739. var objs: any[] = [
  1740. function () { },
  1741. { x: 15, y: 20 },
  1742. null,
  1743. stub,
  1744. "function"
  1745. ];
  1746. jQuery.each(objs, function (i) {
  1747. var isFunc = jQuery.isFunction(objs[i]);
  1748. $("span").eq(i).text(isFunc);
  1749. });
  1750. $.isFunction(function () { });
  1751. }
  1752. function test_isNumeric() {
  1753. $.isNumeric("-10");
  1754. $.isNumeric(16);
  1755. $.isNumeric(0xFF);
  1756. $.isNumeric("0xFF");
  1757. $.isNumeric("8e5");
  1758. $.isNumeric(3.1415);
  1759. $.isNumeric(+10);
  1760. $.isNumeric(0144);
  1761. $.isNumeric("");
  1762. $.isNumeric({});
  1763. $.isNumeric(NaN);
  1764. $.isNumeric(null);
  1765. $.isNumeric(true);
  1766. $.isNumeric(Infinity);
  1767. $.isNumeric(undefined);
  1768. }
  1769. function test_isPlainObject() {
  1770. $.isPlainObject(document.location);
  1771. jQuery.isPlainObject({});
  1772. jQuery.isPlainObject("test");
  1773. }
  1774. function test_isWindow() {
  1775. $("b").append("" + $.isWindow(window));
  1776. }
  1777. function test_isXMLDoc() {
  1778. jQuery.isXMLDoc(document);
  1779. jQuery.isXMLDoc(document.body);
  1780. }
  1781. function test_jQuery() {
  1782. $('div.foo');
  1783. $('div.foo').click(function () {
  1784. $('span', this).addClass('bar');
  1785. });
  1786. $('div.foo').click(function () {
  1787. $(this).slideUp();
  1788. });
  1789. $.post('url.xml', function (data) {
  1790. var $child = $(data).find('child');
  1791. });
  1792. var foo = { foo: 'bar', hello: 'world' };
  1793. var $foo = $(foo);
  1794. var test1 = $foo.prop('foo');
  1795. $foo.prop('foo', 'foobar');
  1796. var test2 = $foo.prop('foo');
  1797. $foo.data('keyName', 'someValue');
  1798. console.log($foo);
  1799. $foo.bind('eventName', function () {
  1800. console.log('eventName was called');
  1801. });
  1802. $foo.trigger('eventName');
  1803. $foo.triggerHandler('eventName');
  1804. $("div > p").css("border", "1px solid gray");
  1805. $("input:radio", document.forms[0]);
  1806. $("div", xml.responseXML);
  1807. $(document.body).css("background", "black");
  1808. $(myForm.elements).hide();
  1809. $('<p id="test">My <em>new</em> text</p>').appendTo('body');
  1810. $('<img />');
  1811. $('<input>');
  1812. var el = $('1<br/>2<br/>3');
  1813. el = $('1<br/>2<br/>3 >');
  1814. $('<input />', {
  1815. type: 'text',
  1816. name: 'test'
  1817. }).appendTo("body");
  1818. $('<input type="text" />').attr({
  1819. name: 'test'
  1820. }).appendTo("body");
  1821. $("<div><p>Hello</p></div>").appendTo("body");
  1822. $("<div/>", {
  1823. "class": "test",
  1824. text: "Click me!",
  1825. click: function () {
  1826. $(this).toggleClass("test");
  1827. }
  1828. }).appendTo("body");
  1829. jQuery(function ($) {
  1830. });
  1831. }
  1832. function test_jquery() {
  1833. var a = { what: "A regular JS object" },
  1834. b = $('body');
  1835. if (a.jquery) {
  1836. alert(' a is a jQuery object! ');
  1837. }
  1838. if (b.jquery) {
  1839. alert(' b is a jQuery object! ');
  1840. }
  1841. alert('You are running jQuery version: ' + $.fn.jquery);
  1842. }
  1843. function test_keydown() {
  1844. $('#target').keydown(function () {
  1845. alert('Handler for .keydown() called.');
  1846. });
  1847. $('#other').click(function () {
  1848. $('#target').keydown();
  1849. });
  1850. var xTriggered = 0;
  1851. $('#target').keydown(function (event) {
  1852. if (event.which == 13) {
  1853. event.preventDefault();
  1854. }
  1855. xTriggered++;
  1856. var msg = 'Handler for .keydown() called ' + xTriggered + ' time(s).';
  1857. });
  1858. $('#other').click(function () {
  1859. $('#target').keydown();
  1860. });
  1861. }
  1862. function test_keypress() {
  1863. $("#target").keypress(function () {
  1864. alert("Handler for .keypress() called.");
  1865. });
  1866. $('#other').click(function () {
  1867. $("#target").keypress();
  1868. });
  1869. $("#other").click(function () {
  1870. $("#target").keypress();
  1871. });
  1872. }
  1873. function test_keyup() {
  1874. $('#target').keyup(function () {
  1875. alert('Handler for .keyup() called.');
  1876. });
  1877. $('#other').click(function () {
  1878. $('#target').keyup();
  1879. });
  1880. $('#other').click(function () {
  1881. $('#target').keyup();
  1882. });
  1883. }
  1884. function test_last() {
  1885. $('li').last().css('background-color', 'red');
  1886. $("p span").last().addClass('highlight');
  1887. }
  1888. function test_length() {
  1889. $(document.body).click(function () {
  1890. $(document.body).append($("<div>"));
  1891. var n = $("div").length;
  1892. $("span").text("There are " + n + " divs." + "Click to add more.");
  1893. }).trigger('click');
  1894. }
  1895. /* deprecated
  1896. function test_live() {
  1897. $(selector).live(events, data, handler);
  1898. $(document).delegate(selector, events, data, handler);
  1899. $(document).on(events, selector, data, handler);
  1900. $("a.offsite").live("click", function () { alert("Goodbye!"); });
  1901. $(document).delegate("a.offsite", "click", function () { alert("Goodbye!"); });
  1902. $(document).on("click", "a.offsite", function () { alert("Goodbye!"); });
  1903. } */
  1904. function test_load() {
  1905. $('#result').load('ajax/test.html');
  1906. $('#result').load('ajax/test.html', function () {
  1907. alert('Load was performed.');
  1908. });
  1909. $('#result').load('ajax/test.html #container');
  1910. $('#b').load('article.html #target');
  1911. $("#success").load("/not-here.php", function (response, status, xhr) {
  1912. if (status == "error") {
  1913. var msg = "Sorry but there was an error: ";
  1914. $("#error").html(msg + xhr.status + " " + xhr.statusText);
  1915. }
  1916. });
  1917. $("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] });
  1918. $("#feeds").load("feeds.php", { limit: 25 }, function () {
  1919. alert("The last 25 entries in the feed have been loaded");
  1920. });
  1921. }
  1922. function test_loadEvent() {
  1923. $('#book').load(function () { });
  1924. $('img.userIcon').load(function () {
  1925. if ($(this).height() > 100) {
  1926. $(this).addClass('bigImg');
  1927. }
  1928. });
  1929. }
  1930. function test_mousedown() {
  1931. $('#target').mousedown(function () {
  1932. alert('Handler for .mousedown() called.');
  1933. });
  1934. $('#other').click(function () {
  1935. $('#target').mousedown();
  1936. });
  1937. }
  1938. function test_mouseenter() {
  1939. $('#outer').mouseenter(function () {
  1940. $('#log').append('<div>Handler for .mouseenter() called.</div>');
  1941. });
  1942. $('#other').click(function () {
  1943. $('#outer').mouseenter();
  1944. });
  1945. var n = 0;
  1946. $("div.enterleave").mouseenter(function () {
  1947. $("p:first", this).text("mouse enter");
  1948. $("p:last", this).text(++n);
  1949. }).mouseleave(function () {
  1950. $("p:first", this).text("mouse leave");
  1951. });
  1952. }
  1953. function test_mouseleave() {
  1954. $('#outer').mouseleave(function () {
  1955. $('#log').append('<div>Handler for .mouseleave() called.</div>');
  1956. });
  1957. $('#other').click(function () {
  1958. $('#outer').mouseleave();
  1959. });
  1960. var i = 0;
  1961. $("div.overout").mouseover(function () {
  1962. $("p:first", this).text("mouse over");
  1963. }).mouseout(function () {
  1964. $("p:first", this).text("mouse out");
  1965. $("p:last", this).text(++i);
  1966. });
  1967. var n = 0;
  1968. $("div.enterleave").mouseenter(function () {
  1969. $("p:first", this).text("mouse enter");
  1970. }).mouseleave(function () {
  1971. $("p:first", this).text("mouse leave");
  1972. $("p:last", this).text(++n);
  1973. });
  1974. }
  1975. function test_mousemove() {
  1976. $("#target").mousemove(function (event) {
  1977. var msg = "Handler for .mousemove() called at ";
  1978. msg += event.pageX + ", " + event.pageY;
  1979. $("#log").append("<div>" + msg + "</div>");
  1980. });
  1981. $("#other").click(function () {
  1982. $("#target").mousemove();
  1983. });
  1984. $("div").mousemove(function (e) {
  1985. var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
  1986. var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
  1987. $("span:first").text("( e.pageX, e.pageY ) : " + pageCoords);
  1988. $("span:last").text("( e.clientX, e.clientY ) : " + clientCoords);
  1989. });
  1990. }
  1991. function test_mouseout() {
  1992. $('#outer').mouseout(function () {
  1993. $('#log').append('Handler for .mouseout() called.');
  1994. });
  1995. $('#other').click(function () {
  1996. $('#outer').mouseout();
  1997. });
  1998. var i = 0;
  1999. $("div.overout").mouseout(function () {
  2000. $("p:first", this).text("mouse out");
  2001. $("p:last", this).text(++i);
  2002. }).mouseover(function () {
  2003. $("p:first", this).text("mouse over");
  2004. });
  2005. var n = 0;
  2006. $("div.enterleave").bind("mouseenter", function () {
  2007. $("p:first", this).text("mouse enter");
  2008. }).bind("mouseleave", function () {
  2009. $("p:first", this).text("mouse leave");
  2010. $("p:last", this).text(++n);
  2011. });
  2012. }
  2013. function test_mouseup() {
  2014. $("p").mouseup(function () {
  2015. $(this).append('<span style="color:#F00;">Mouse up.</span>');
  2016. }).mousedown(function () {
  2017. $(this).append('<span style="color:#00F;">Mouse down.</span>');
  2018. });
  2019. $('#target').mouseup(function () {
  2020. alert('Handler for .mouseup() called.');
  2021. });
  2022. $('#other').click(function () {
  2023. $('#target').mouseup();
  2024. });
  2025. $("p").mouseup(function () {
  2026. $(this).append('<span style="color:#F00;">Mouse up.</span>');
  2027. }).mousedown(function () {
  2028. $(this).append('<span style="color:#00F;">Mouse down.</span>');
  2029. });
  2030. }
  2031. function test_mouseover() {
  2032. $('#outer').mouseover(function () {
  2033. $('#log').append('<div>Handler for .mouseover() called.</div>');
  2034. });
  2035. $('#other').click(function () {
  2036. $('#outer').mouseover();
  2037. });
  2038. var i = 0;
  2039. $("div.overout").mouseover(function () {
  2040. $("p:first", this).text("mouse over");
  2041. $("p:last", this).text(++i);
  2042. }).mouseout(function () {
  2043. $("p:first", this).text("mouse out");
  2044. });
  2045. var n = 0;
  2046. $("div.enterleave").mouseenter(function () {
  2047. n += 1;
  2048. $(this).find("span").text("mouse enter x " + n);
  2049. }).mouseleave(function () {
  2050. $(this).find("span").text("mouse leave");
  2051. });
  2052. }
  2053. function test_makeArray() {
  2054. var elems = document.getElementsByTagName("div");
  2055. var arr = jQuery.makeArray(elems);
  2056. arr.reverse();
  2057. $(arr).appendTo(document.body);
  2058. var obj = $('li');
  2059. var arr = $.makeArray(obj);
  2060. jQuery.isArray(arr) === true;
  2061. }
  2062. function test_map() {
  2063. $(':checkbox').map(function () {
  2064. return this.id;
  2065. }).get().join(',');
  2066. $("p").append($("input").map(function () {
  2067. return $(this).val();
  2068. }).get().join(", "));
  2069. var mappedItems = $("li").map(function (index) {
  2070. var replacement = $("<li>").text($(this).text()).get(0);
  2071. if (index == 0) {
  2072. $(replacement).text($(replacement).text().toUpperCase());
  2073. } else if (index == 1 || index == 3) {
  2074. replacement = null;
  2075. } else if (index == 2) {
  2076. replacement = [replacement, $("<li>").get(0)];
  2077. $(replacement[0]).append("<b> - A</b>");
  2078. $(replacement[1]).append("Extra <b> - B</b>");
  2079. }
  2080. return replacement;
  2081. });
  2082. $("#results").append(mappedItems);
  2083. var fakeArray = { "length": 1, 0: "Addy", 1: "Subtracty" };
  2084. var realArray = $.makeArray(fakeArray)
  2085. $.map(realArray, function (val, i) { });
  2086. var arr = ["a", "b", "c", "d", "e"];
  2087. $("div").text(arr.join(", "));
  2088. arr = jQuery.map(arr, function (n, i) {
  2089. return (n.toUpperCase() + i);
  2090. });
  2091. $("p").text(arr.join(", "));
  2092. arr = jQuery.map(arr, function (a) {
  2093. return a + a;
  2094. });
  2095. $("span").text(arr.join(", "));
  2096. $.map([0, 1, 2], function (n) {
  2097. return n + 4;
  2098. });
  2099. $.map([0, 1, 2], function (n) {
  2100. return n > 0 ? n + 1 : null;
  2101. });
  2102. $.map([0, 1, 2], function (n) {
  2103. return [n, n + 1];
  2104. });
  2105. var dimensions = { width: 10, height: 15, length: 20 };
  2106. dimensions = $.map(dimensions, function (value, index) {
  2107. return value * 2;
  2108. });
  2109. var dimensions = { width: 10, height: 15, length: 20 },
  2110. keys = $.map(dimensions, function (value, index) {
  2111. return index;
  2112. });
  2113. $.map([0, 1, 2, 3], function (a) {
  2114. return a * a;
  2115. });
  2116. $.map([0, 1, 52, 97], function (a) {
  2117. return (a > 50 ? a - 45 : null);
  2118. });
  2119. var array = [0, 1, 52, 97];
  2120. array = $.map(array, function (a, index) {
  2121. return [a - 45, index];
  2122. });
  2123. }
  2124. function test_merge() {
  2125. var oldArray: any[];
  2126. var newArray = $.merge([], oldArray);
  2127. $.merge([0, 1, 2], [2, 3, 4]);
  2128. var first = ['a', 'b', 'c'];
  2129. var second = ['d', 'e', 'f'];
  2130. $.merge($.merge([], first), second);
  2131. }
  2132. function test_prop() {
  2133. var $input = $(this);
  2134. $("p").html(".attr('checked'): <b>" + $input.attr('checked') + "</b><br>"
  2135. + ".prop('checked'): <b>" + $input.prop('checked') + "</b><br>"
  2136. + ".is(':checked'): <b>" + $input.is(':checked')) + "</b>";
  2137. $("input").prop("disabled", false);
  2138. $("input").prop("checked", true);
  2139. $("input").val("someValue");
  2140. $("input[type='checkbox']").prop("checked", function (i, val) {
  2141. return !val;
  2142. });
  2143. $("input[type='checkbox']").prop({
  2144. disabled: true
  2145. });
  2146. var title: string = $('option:selected', this).prop('title');
  2147. }
  2148. function test_text() {
  2149. var str = $("p:first").text();
  2150. $("p:last").html(str);
  2151. $('ul li').text(function (index) {
  2152. return 'item number ' + (index + 1);
  2153. });
  2154. $("p").text("<b>Some</b> new text.");
  2155. }