博主信息
博文 33
粉丝 0
评论 0
访问量 35121
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
前端 - jQuery - 事件和Ajax
原创
939人浏览过

前端 - jQuery - 事件和Ajax

一、事件

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <script src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
  8. </head>
  9. <body>
  10. <ul>
  11. <li>01</li>
  12. <li>02</li>
  13. <li>03</li>
  14. <li>04</li>
  15. </ul>
  16. <form action="">
  17. <input type="email" name="" id="" />
  18. <input type="password" name="" id="" />
  19. <button type="submit">提交</button>
  20. </form>
  21. </body>
  22. <script>
  23. // 3. 事件委派
  24. $("ul").delegate("li", "click", function () {
  25. alert("hello");
  26. });
  27. // 4. 事件切换
  28. $("ul li:first-child").hover(
  29. function () {
  30. $(this).prop("style", "color: red;");
  31. },
  32. function () {
  33. $(this).prop("style", "color: blue;");
  34. }
  35. );
  36. // 5. 事件
  37. //当元素失去焦点时触发
  38. $("form input[type='email']").blur(function () {
  39. alert("hello");
  40. });
  41. //当元素被点击时触发
  42. $("form input[type='email']").click(function () {
  43. alert("world");
  44. });
  45. //当提交表单时触发
  46. $("form").submit(function () {
  47. alert("已提交");
  48. });
  49. </script>
  50. </html>

二、Ajax

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <script src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
  8. </head>
  9. <body>
  10. <form>
  11. <input type="email" name="email" />
  12. <input type="password" name="password" />
  13. <button type="button">提交</button>
  14. </form>
  15. <div></div>
  16. </body>
  17. <script>
  18. // Ajax操作
  19. $("form button").click(function () {
  20. //序列表表格内容为字符串
  21. var data = $("form").serialize();
  22. console.log(data);
  23. $.ajax({
  24. url: "login.php",
  25. type: "POST",
  26. data: data,
  27. dataType: "json",
  28. success: function (res) {
  29. console.log(res);
  30. var str = res.email + res.password;
  31. $("div").html(str);
  32. },
  33. });
  34. });
  35. </script>
  36. </html>
  1. <?php
  2. $email = $_POST['email'];
  3. $password = $_POST['password'];
  4. $arr = array("email"=>$email, "password"=>$password);
  5. $json_obj = json_encode($arr);
  6. echo $json_obj;

四、课程总结

  • 今天进行了 jQuery 的事件处理和Ajax操作,通过上课认真听讲和认真完成老师布置的作业,使得我对 jQuery的理解和运用更加深入和熟悉。最主要的知识点是明白和掌握了事件处理和Ajax的特点以及基本用法。
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学