PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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

Large files files are truncated, but you can click here to view the full file

  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. })

Large files files are truncated, but you can click here to view the full file