Startkit. Ajax

Source: Internet
Author: User

  1. // Ajax application
  2. // My God, I cannot understand PHP. Forget it. Find an example of Asp.net.
  3. /*
  4. Use jquery to generate a div container with the ID "rating ".
  5. The Code generates five links and appends them to the "rating" container. When one of the links is clicked,
  6. The score indicated by this link is sent to rate. php as the rating parameter. Then,
  7. The results will be passed back from the server in XML format and added to the container to replace these links.
  8. */
  9. $ (Document). Ready (function (){
  10. // Generate Markup
  11. VaR ratingmarkup = ["Please rate:"];
  12. For (VAR I = 1; I <= 5; I ++ ){
  13. Ratingmarkup [ratingmarkup. Length] = "<a href = '#'>" + I + "</a> ";
  14. }
  15. // Add markup to container and applier click handlers to anchors
  16. $ ("# Rating"). append (ratingmarkup. Join (''). Find (" A "). Click (function (e ){
  17. E. preventdefault ();
  18. // Send requests
  19. $. Post ("rate. php", {rating: rating (this).html ()}, function (XML ){
  20. // Format result
  21. VaR result = [
  22. "Thanks for rating, current average :",
  23. $ ("Average", XML). Text (),
  24. ", Number of votes :",
  25. $ ("Count", XML). Text ()
  26. ];
  27. // Output result
  28. $ ("# Rating" pai.html (result. Join (''));
  29. });
  30. });
  31. });
  32. /*
  33. An issue that often occurs when using Ajax to load content is: when loading an event handle to an HTML document,
  34. You also need to apply these events on the loaded content. You have to apply these event handles after the content is loaded,
  35. To prevent repeated code execution, you may use
  36. */
  37. // Lets use the shortcut
  38. $ (Function (){
  39. VaR addclickhandlers = function (){
  40. $ ("A. clickmetoloadcontent"). Click (function (){
  41. $ ("# Target"). Load (this. href, addclickhandlers );
  42. });
  43. };
  44. Addclickhandlers ();
  45. });
  46. /*
  47. Now, addclickhandlers is executed only once after the Dom is loaded. This is after you click a link with the clickmetoloadcontent style and the content is loaded.
  48. Note that the addclickhandlers function is defined as a local variable instead of a global variable (for example, function addclickhandlers (){...}),
  49. This is done to prevent conflicts with other global variables or functions.
  50. Another common problem is the callback parameter. You can use an additional parameter to specify the callback method. The simple method is to include this callback method in another function:
  51. */
  52. $ (Function (){
  53. // Get some data
  54. VaR foobar = ...;
  55. // Specify handler, it needs data as a paramter
  56. VaR handler = function (data ){
  57. ...
  58. };
  59. // Add click handler and pass foobar!
  60. $ ('A'). Click (function (event) {handler (foobar );});
  61. // If you need the context of the original handler, use apply:
  62. $ ('A'). Click (function (event) {handler. Apply (this, [foobar]);});
  63. });
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.