You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index-2.html 3.1 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>垃圾分类查询助手</title>
  6. <style>
  7. .search, .result {
  8. width: 720px;
  9. margin: 50px auto;
  10. }
  11. .search > input {
  12. width: 520px;
  13. border: none;
  14. outline: none;
  15. text-align: center;
  16. font-size: 36px;
  17. line-height: 36px;
  18. border-bottom: 1px solid gray;
  19. margin: 0 20px;
  20. }
  21. .search button {
  22. background-color: red;
  23. color: white;
  24. font-size: 28px;
  25. border: none;
  26. outline: none;
  27. width: 120px;
  28. }
  29. .result > p, .result > div {
  30. width: 640px;
  31. margin: 0 auto;
  32. }
  33. .result > p, .result span {
  34. text-align: left;
  35. font-size: 28px;
  36. }
  37. .result img {
  38. vertical-align: middle;
  39. }
  40. .explain {
  41. font-size: 12px;
  42. color: darkgray;
  43. }
  44. .result .pre {
  45. font-size: 16px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="app">
  51. <div class="search">
  52. <input type="text" placeholder="请输入垃圾名字" v-model.trim="word" @keydown.enter="search()">
  53. <button @click="search()">查询</button>
  54. </div>
  55. <div class="result">
  56. <p v-if="searched && !results">没有对应的查询结果</p>
  57. <div v-for="result in results">
  58. <p>
  59. <img :src="'images/' + pictures[result.type]" width="56" :alt="types[result.type]">
  60. &nbsp;&nbsp;
  61. <span>{{ result.name }}</span>
  62. &nbsp;&nbsp;
  63. <span class="pre" v-if="result.aipre == 1">(预测结果)</span>
  64. </p>
  65. <p class="explain">说明:{{ result.explain }}</p>
  66. </div>
  67. </div>
  68. </div>
  69. <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
  70. <script>
  71. new Vue({
  72. el: '#app',
  73. data: {
  74. word: '',
  75. searched: false,
  76. types: ['可回收物', '有害垃圾', '厨余垃圾', '其他垃圾'],
  77. pictures: ['recyclable.png', 'harmful-waste.png', 'kitchen-waste.png', 'other-waste.png'],
  78. results: []
  79. },
  80. methods: {
  81. search() {
  82. if (this.word.trim().length > 0) {
  83. let key = 'e8c5524dd2a365f20908ced735f8e480'
  84. let url = `http://api.tianapi.com/txapi/lajifenlei/?key=${key}&word=${this.word}`
  85. fetch(url)
  86. .then(resp => resp.json())
  87. .then(json => {
  88. this.searched = true
  89. this.results = json.newslist
  90. })
  91. }
  92. }
  93. }
  94. })
  95. </script>
  96. </body>
  97. </html>

项目描述测试

Contributors (2)