回顾第一弹

js脚本编写教程,第一弹,jQuery调用api接口

前叙

js脚本编写教程,第一弹,jQuery调用api接口
使用的api接口网站:https://www.tianapi.com/
免费版一天100次,个人够用。

实操

第一个练习接口:http://api.tianapi.com/dyvideohot/index

套用上次模板就可以返回数据:

![6991a55e33d6670c229cdded1d05d48f.png](:/4435f3a9d0fc48b0ad04bb472c0d0b1e)

取到数据就可以出来,将其在页面显示。

第二个练习接口:http://api.tianapi.com/douyinhot/index

代码页:

再来一个带参数的接口:http://api.tianapi.com/sicprobe/index

附代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #button {
  14. width: 50px;
  15. height: 50px;
  16. }
  17. #text {
  18. height: 50px;
  19. border: 1px solid #000;
  20. background-color: aliceblue;
  21. }
  22. input {
  23. float: left;
  24. }
  25. #text:active {
  26. content: "";
  27. height: 1px;
  28. }
  29. ul {
  30. width: 900px;
  31. margin: 0 auto;
  32. }
  33. li {
  34. width: 200px;
  35. height: 200px;
  36. list-style: none;
  37. /* border: 0px solid #000; */
  38. float: left;
  39. /* background-color: antiquewhite; */
  40. }
  41. #ll {
  42. position: relative;
  43. }
  44. #ll img {
  45. position: absolute;
  46. bottom: 16px;
  47. }
  48. #z12 {
  49. position: absolute;
  50. font-size: 12px;
  51. bottom: 0px;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div style="overflow: hidden">
  57. <input type="text" id="text" value="" />
  58. <input type="button" id="button" value="搜索" />
  59. </div>
  60. <ul>
  61. <!-- 一级导航 -->
  62. </ul>
  63. <div id="pot">
  64. <!-- 二级导航 -->
  65. <!-- 分块,用ul表示 -->
  66. </div>
  67. </body>
  68. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  69. <script>
  70. //eb59864e6e6e0c08d7ac6dab6dfa9b5e
  71. //页面需引入jquery库 https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
  72. $(document).ready(function () {
  73. let oInput = document.getElementById("text");
  74. let oUL = document.querySelector("ul");
  75. let oDiv = document.getElementById("pot");
  76. $("#button").click(function () {
  77. //点击页面上id为button的按钮发送请求
  78. let value = oInput.value;
  79. console.log(value);
  80. $.post(
  81. "http://api.tianapi.com/sicprobe/index",
  82. {
  83. key: "eb59864e6e6e0c08d7ac6dab6dfa9b5e",
  84. num: "10",
  85. word: value,
  86. },
  87. function (data, status) {
  88. // console.log(data);
  89. // console.log(data.newslist);
  90. // console.log(data.newslist[0]);
  91. // console.log(data.newslist[0].picUrl);
  92. // console.log(data.newslist.length);
  93. let dataa = data.newslist;
  94. let str = "";
  95. if (dataa) {
  96. dataa.forEach((item) => {
  97. str += `
  98. <li id='ll'>
  99. <p href="">${item.title}</p>
  100. <br>
  101. <img src="${item.picUrl}" alt="">
  102. <p id='z12'>${item.ctime}</p>
  103. </li>
  104. `;
  105. // console.log(item.picUrl);
  106. });
  107. }
  108. oUL.innerHTML = str;
  109. // console.log(str);
  110. }
  111. );
  112. });
  113. });
  114. </script>
  115. </html>

这个主要是通过获取搜索框内的值,传参来调接口搜索。