博客列表 >jsonp的演示及事件代理的案例

jsonp的演示及事件代理的案例

简行
简行原创
2020年08月19日 17:50:20603浏览

一.jsonp的演示

1.HTMLindex.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>jsonp请求</title>
  7. </head>
  8. <style>
  9. table {
  10. border: 1px solid;
  11. text-align: center;
  12. margin-top: 20px;
  13. }
  14. td {
  15. border: 1px solid;
  16. }
  17. thead {
  18. text-align: left;
  19. padding: 0 10px;
  20. }
  21. div {
  22. margin: 10px 500px;
  23. }
  24. </style>
  25. <body>
  26. <div>
  27. <button>获取数据</button>
  28. <table></table>
  29. </div>
  30. </body>
  31. <script>
  32. function handle(data) {
  33. var table = document.querySelector("table");
  34. var obj = JSON.parse(data);
  35. console.log(obj);
  36. var ta = "<thead>" + obj.title + "</thead>";
  37. ta += "<tr><td>编号</td><td>名称</td> <td>价格</td><td>总价</td></tr>";
  38. ta +=
  39. "<tr><td>" +
  40. obj.info.id +
  41. "</td><td>" +
  42. obj.info.name +
  43. "</td><td>" +
  44. obj.info.price +
  45. obj.info.unit +
  46. "</td><td>" +
  47. obj.info.price * obj.info.num +
  48. "</td></tr>";
  49. table.innerHTML = ta;
  50. }
  51. var btn = document.querySelector("button");
  52. btn.addEventListener("click", function () {
  53. //新建script标签
  54. var script = document.createElement("script");
  55. //注:回调函数名(例:&jsp=handle)一定要写在URL最后
  56. script.src = "http://jsonp.cn/Jsonp.php?id=3&jsp=handle";
  57. document.head.appendChild(script);
  58. });
  59. </script>
  60. </html>

2.后端Jsonp.php:已设置在http://jsonp.cn域名下

  1. <?php
  2. //设置编码格式请求头
  3. header('Content-Type:text/html;charset=utf-8 ');
  4. //获取参数
  5. $id = $_GET['id'];
  6. //前端回调函数名称
  7. $callback = $_GET['jsp'];
  8. //模拟获取到数据库的数据
  9. $goods =[
  10. '{"id":GD1002,"name":苹果,"price":8,"unit":元/斤,"num":6}',
  11. '{"id":GD1042,"name":哈密瓜,"price":10.5,"unit":个,"num":4',
  12. '{"id":"GD1026","name":"西瓜","price":6.5,"unit":"个","num":10}',
  13. '{"id":GD1034,"name":樱桃,"price":125,"unit":箱,"num":7}'
  14. ];
  15. //判断$id是否在数组的所有当中
  16. if(array_key_exists($id-1,$goods)){
  17. $good = $goods[$id-1];
  18. }
  19. $json='{"title":"商品信息表","info":'.$good.'}';
  20. echo $callback."(".json_encode($json).")";

二.事件代理的案例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>事件代理</title>
  7. </head>
  8. <body>
  9. <div>
  10. <button>按钮1</button>
  11. <button>按钮2</button>
  12. <button>按钮3</button>
  13. <button>按钮4</button>
  14. </div>
  15. </body>
  16. <script>
  17. //绑定父级点击事件
  18. document.querySelector("div").addEventListener("click", function (eve) {
  19. addclick(eve);
  20. });
  21. function addclick(eve) {
  22. //target:触发事件元素
  23. //currentTarget:绑定事件元素
  24. //tagName:获取元素名称
  25. alert(
  26. "当前点击的是:" +
  27. eve.target.innerText +
  28. ";绑定点击元素是:" +
  29. eve.currentTarget.tagName
  30. );
  31. }
  32. </script>
  33. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议