[关闭]
@xunuo 2018-08-13T15:49:33.000000Z 字数 2356 阅读 960

Wikipedia Entrance

前端


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet">
  6. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
  7. <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
  8. <title></title>
  9. <style>
  10. body {
  11. background-color: #092B40;
  12. }
  13. h1 {
  14. text-align: center;
  15. color: #FFFFFF;
  16. }
  17. a {
  18. margin-left: 43%;
  19. text-align: center;
  20. }
  21. .input-group-design {
  22. margin: 50px 30%;
  23. font-size: 16px;
  24. }
  25. #result ul {
  26. list-style: none;
  27. padding: 0;
  28. }
  29. #result li {
  30. color: black;
  31. font-weight: 300px;
  32. padding-left: 6px;
  33. }
  34. #result li a {
  35. padding: 16px 16px 2px 24px;
  36. margin: 20px 200px;
  37. font-weight: bold;
  38. display: block;
  39. background-color: #E6E6E6;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <h1>Wikipedia Entrance</h1>
  45. <a href="http://en.wikipedia.org/wiki/Special:Random">Click Here For A Random Wikipedia Page</a>
  46. <div class="input-group-design">
  47. <div class="input-group">
  48. <input id="input-text" type="text" class="form-control">
  49. <span class="input-group-btn">
  50. <button id="search-btn" class="btn btn-default">GO</button>
  51. </span>
  52. </div>
  53. </div>
  54. <div>
  55. <selection id="result">
  56. </selection>
  57. </div>
  58. <script type="text/javascript">
  59. var paraJack = {
  60. wikiItemBaseUrl: 'https://en.wikipedia.org/wiki/',
  61. resultNum: 12 /*总共展示12条数据*/
  62. }
  63. $('#search-btn').click(function() { /*点击GO按钮时*/
  64. var searchStr = document.getElementById('input-text').value; /*取输入框中的值*/
  65. console.log(searchStr);
  66. $.getJSON('https://en.wikipedia.org/w/api.php?action=query&titles=' +
  67. encodeURIComponent(searchStr) + '&action=query&format=json&list=search&srsearch=' +
  68. encodeURIComponent(searchStr) + '&srlimit=' + paraJack.resultNum + '&srprop=snippet&callback=?',
  69. function(data) {
  70. genResultDom(paraJack.resultNum);
  71. var $anchors = $('#result a');
  72. $.each(data.query.search, function(index, val) {
  73. var $theAnchor = $anchors.eq(index),
  74. $p = $theAnchor.find('p'),
  75. $h3 = $theAnchor.find('h3'),
  76. title = val.title, //结果项标题
  77. snippet = val.snippet; //结果项内容提要
  78. $h3.html(title);
  79. $p.html(snippet);
  80. $theAnchor.attr({
  81. href: paraJack.wikiItemBaseUrl + title
  82. //用结果项标题生成URL
  83. });
  84. });
  85. $('#result').show(); //显示结果
  86. });
  87. });
  88. function genResultDom(resultNum) {
  89. if(typeof resultNum === 'number') {
  90. var $result = $('#result'),
  91. $ul = $result.html('<ul></ul>').find('ul');
  92. for(var i = 0; i < resultNum; i++) {
  93. $ul.append($('<li><a href="" target="_blank"><h3></h3><p></p></a></li>'));
  94. }
  95. }
  96. }
  97. </script>
  98. </body>
  99. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注