搜索页面效果
需求分析:
1.默认显示搜索历史记录,搜索记录倒序排列且去除重复值。
2.点击搜索后,显示搜索列表,历史记录隐藏。
3.搜索值变化时,列表隐藏。搜索值为空时,历史记录显示。
4.点击历史记录,进行相应的搜索。点击删除时清空搜索历史记录
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | <! DOCTYPE html> < html lang="en"> < head > < meta charset="UTF-8"> < meta http-equiv="X-UA-Compatible" content="IE=edge"> < meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" /> < title >Document</ title > < style > * { padding: 0; margin: 0; } ul { list-style: none; } .container { padding: 0 4vw } .search { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; margin-top: 5.067vw } .input-container { position: relative } .input-container span { position: absolute; /* top: 1.867vw; */ width: 4.8vw; /* height: 4.8vw; */ } .input-container .search-icon { left: 4.267vw; top: 1.967vw; } .input-container .search-icon svg, .input-container .cancel-icon svg { width: 4.8vw; height: auto; } .input-container .cancel-icon { right: 4.267vw; top: 2.1vw; } .search input { width: 82.4vw; height: 8.533vw; padding-left: 11.2vw; border-radius: 45.867vw; background: #f7f7f7; float: right; border: none; outline: medium; -webkit-box-sizing: border-box; box-sizing: border-box } .search button { font-size: 3.7vw; color: #444; background: none; border: none } .history { padding-bottom: 4.267vw; background: #fff; -webkit-box-shadow: 0 -1px rgba(0, 0, 0, .07843) inset; box-shadow: inset 0 -1px rgba(0, 0, 0, .07843) } .history .title { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; margin: 3.733vw 0 5.333vw } .history .title span { color: #444; font-family: PingFang SC; font-weight: 700; font-size: 4.267vw } .history .title img { width: 4.4vw; height: auto; } .history .delete { width: 4.4vw; height: auto; } .history .delete svg { width: 4.4vw; height: auto; } span svg { width: 100%; } .history-list li { display: inline-block; padding: 1.067vw 3.2vw; color: #666; background: #f7f7f7; border-radius: 7.733vw; margin-bottom: 3.2vw } .all, .history-list li { text-align: center; font-family: PingFang SC; font-weight: 500; font-size: 3.733vw } .all { padding-top: 1.067vw; color: #999 } /* 列表 */ .item { padding: 3.2vw 4vw; margin-top: 4.8vw; background-color: #fff; border-radius: 2.133vw; -webkit-box-shadow: 0 0 1.067vw rgba(0, 0, 0, .12157); box-shadow: 0 0 1.067vw rgba(0, 0, 0, .12157) } .item a { display: block; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; text-decoration: none; } .item .item-image { width: 25.6vw; height: 34.133vw; border-radius: 1.067vw; } .item-image img { width: 100%; height: 100% } .item .item-intro { float: right; width: 55.2vw; height: 34.133vw; -webkit-box-sizing: border-box; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: justify; -ms-flex-pack: justify; justify-content: space-between; box-sizing: border-box } .intro-top .intro-title { color: #444; font-family: PingFang SC; font-weight: 700; font-size: 4.267vw } .intro-top .intro-type { padding-top: 3.2vw; color: #999; font-family: PingFang SC; font-weight: 500; font-size: 3.733vw } .price { -webkit-box-align: center; -ms-flex-align: center; align-items: center; color: #ed9027; font-family: PingFang SC; font-weight: 700; font-size: 4.267vw } button { border: none } .intro-bottom { color: #ed9027 } .pre-order { float: right; margin-right: 4vw; width: 12.8vw; height: 6.4vw; border-radius: 3.2vw; background: #ed9027; font-size: 3.2vw; -webkit-box-align: center; -ms-flex-align: center; align-items: center; color: #fff } /* 搜索框去掉默认取消按钮 */ input[type=search]::-webkit-search-cancel-button { -webkit-appearance: none; } /* ios自带搜索图标 */ input[type="search"] { -webkit-appearance: none; } input::-webkit-search-cancel-button { display: none; } .attention { display: none; text-align: center; font-size: 3.2vw; margin-top: 4vw; padding-top: 1.067vw; color: #999; } </ style > </ head > < body > < div class="container"> < div class="search"> < div class="input-container"> < input type="search" placeholder="请输入您要搜索的内容" onsearch="searchData()" class="search-val" oninput="changeVal()"> < span class="search-icon"> < svg t="1661304760463" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3718" width="200" height="200"> < path d="M966.4 924.8l-230.4-227.2c60.8-67.2 96-156.8 96-256 0-217.6-176-390.4-390.4-390.4-217.6 0-390.4 176-390.4 390.4 0 217.6 176 390.4 390.4 390.4 99.2 0 188.8-35.2 256-96l230.4 227.2c9.6 9.6 28.8 9.6 38.4 0C979.2 950.4 979.2 934.4 966.4 924.8zM102.4 441.6c0-185.6 150.4-339.2 339.2-339.2s339.2 150.4 339.2 339.2c0 89.6-35.2 172.8-92.8 233.6-3.2 0-3.2 3.2-6.4 3.2-3.2 3.2-3.2 3.2-3.2 6.4-60.8 57.6-144 92.8-233.6 92.8C256 780.8 102.4 627.2 102.4 441.6z" p-id="3719" fill="#8a8a8a"></ path > </ svg > </ span > <!-- <img class="search-icon" src="../addons/hedawei_shopv2/template/mobile/default/static/images/hedawei/sear.png" alt=""> --> <!-- <img class="cancel-icon" src="../addons/hedawei_shopv2/template/mobile/default/static/images/hedawei/cancel.png" alt=""> --> < span class="cancel-icon"> < svg t="1661305053945" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4672" width="200" height="200"> < path d="M512 896c212.1 0 384-171.9 384-384S724.1 128 512 128 128 299.9 128 512s171.9 384 384 384z m-0.8-427.3l150.3-150.3 45.2 45.2L556.5 514l147.3 147.3-45.2 45.2-147.4-147.3-146 146L320 660l146-146-149.6-149.6 45.2-45.2 149.6 149.5z" p-id="4673" fill="#8a8a8a"></ path > </ svg > </ span > </ div > < button type="submit" class="cancel">取消</ button > </ div > < div class="list"> < ul > <!-- <li class="item"> <a href=""> <div class="item-image"> <img src="" alt=""> </div> <div class="item-intro"> <div class="intro-top"> <p class="intro-title">密室逃脱</p> <p class="intro-type">悬疑 | 惊恐 </p> </div> <div class="intro-bottom"> <p> <span>¥198/人</span> <button class="pre-order">预定</button> </p> </div> </div> </a> </li> --> </ ul > </ div > < p class="attention">暂无数据</ p > < div class="history"> < div class="title"> < p >历史记录</ p > < span class="delete"> < svg t="1661304680168" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2624" width="200" height="200"> < path d="M909.050991 169.476903l-217.554898 0 0-31.346939c0-39.5866-32.205493-71.792093-71.793116-71.792093L408.15591 66.337871c-39.5866 0-71.792093 32.205493-71.792093 71.792093l0 31.346939L113.349581 169.476903c-11.013845 0-19.942191 8.940626-19.942191 19.954471s8.928347 19.954471 19.942191 19.954471l84.264149 0 0 640.687918c0 60.479443 49.203632 109.683075 109.683075 109.683075l416.474366 0c60.479443 0 109.683075-49.203632 109.683075-109.683075L833.454246 209.385844l75.595722 0c11.012821 0 19.942191-8.940626 19.942191-19.954471S920.063813 169.476903 909.050991 169.476903zM376.2482 138.130987c0-17.593703 14.314007-31.907711 31.907711-31.907711l211.547067 0c17.593703 0 31.907711 14.314007 31.907711 31.907711l0 31.346939L376.2482 169.477926 376.2482 138.130987zM793.569864 850.074785c0 38.486546-31.312146 69.798692-69.798692 69.798692L307.297828 919.873478c-38.486546 0-69.798692-31.312146-69.798692-69.798692L237.499136 211.042577l556.070728 0L793.569864 850.074785z" p-id="2625" fill="#8a8a8a"></ path > < path d="M510.662539 861.276918c11.012821 0 19.954471-8.92937 19.954471-19.942191L530.61701 294.912753c0-11.013845-8.94165-19.942191-19.954471-19.942191s-19.954471 8.928347-19.954471 19.942191L490.708068 841.334727C490.708068 852.347548 499.649717 861.276918 510.662539 861.276918z" p-id="2626" fill="#8a8a8a"></ path > < path d="M374.562814 801.449321c11.012821 0 19.954471-8.92937 19.954471-19.942191L394.517285 354.74035c0-11.013845-8.94165-19.942191-19.954471-19.942191s-19.954471 8.928347-19.954471 19.942191l0 426.76678C354.608344 792.519951 363.549993 801.449321 374.562814 801.449321z" p-id="2627" fill="#8a8a8a"></ path > < path d="M649.832182 801.449321c11.012821 0 19.954471-8.92937 19.954471-19.942191L669.786653 354.74035c0-11.013845-8.94165-19.942191-19.954471-19.942191s-19.954471 8.928347-19.954471 19.942191l0 426.76678C629.877711 792.519951 638.81936 801.449321 649.832182 801.449321z" p-id="2628" fill="#8a8a8a"></ path > </ svg > </ span > <!-- <img src="../addons/hedawei_shopv2/template/mobile/default/static/images/hedawei/delete.png" alt="" class="delete"> --> </ div > < ul class="history-list"> <!-- <li>测试数据11111</li> --> </ ul > < p class="all">全部历史记录</ p > </ div > </ div > < script type="text/javascript" src="../addons/hedawei_shopv2/static/js/dist/jquery/jquery-1.11.1.min.js"></ script > < script > storage() // 历史记录本地存储 let history = localStorage.getItem('history') || '' function searchData() { let temp = $('.search-val').val() if (temp) { history = history.length ? `${temp},${history}` : `${temp}` localStorage.setItem('history', history) storage() getData() } $('.history').css('display', 'none') // window.location.href=`http://hdong.com/app/index.php?i=36&c=entry&m=hedawei_shopv2&do=mobile&r=ssresult&search=${temp}` // window.location.href = "{php echo mobileUrl('ssresult')}&search=" + temp; } $('.delete').click(function () { localStorage.removeItem('history') $('.history-list').empty() history = [] }) // 搜索内容清空 $('.cancel-icon').click(function () { // console.log($('.search-val').val(), 'val'); $('.search-val').val('') $('.history').css('display', 'block') $('.list ul').css('display', 'none') $('.attention').css('display', 'none') }) // 返回上一页 $('.cancel').click(function () { window.history.go(-1) }) function getData() { $.ajax({ url: "{php echo mobileUrl('goodsall')}", type: 'get', dataType: 'json', data: { words: $('.input-container input').val(), //非必须 标题搜索 categoryid: '' //非必须 分类id搜索 }, success: function (data) { // console.log(data) // 动态添加元素 $('.list ul').html(""); if (!data.length) { $('.attention').css('display', 'block') } else { $('.attention').css('display', 'none') data.forEach(ele => { $('.list ul').append(` < li class="item"> < a href="{php echo mobileUrl('goods/detail')}&id=${ele.id}"> < div class="item-image"> < img src="${ele.thumb}" alt=""> </ div > < div class="item-intro"> < div class="intro-top"> < p class="intro-title">${ele.title}</ p > < p class="intro-type">${ele.keywords} </ p > </ div > < div class="intro-bottom"> < p > < span >¥${ele.marketprice}/人</ span > < button class="pre-order" >预订</ button > </ p > </ div > </ div > </ a > </ li >`) }) } } }) } function changeVal() { let searchVal = $('.search-val').val() // console.log(searchVal, 'searchVal'); if (searchVal == '') { // 输入值为空时,历史记录显示 $('.history').css('display', 'block') $('.list ul').css('display', 'none') $('.attention').css('display', 'none') } else { $('.history').css('display', 'none') $('.list ul').css('display', 'block') $('.list ul').html(""); } } function storage() { let currentHistory = localStorage.getItem('history') || '' $('.history-list').empty() if (currentHistory.length) { let tempResult = currentHistory.split(',') let tempResu = [...new Set(tempResult)] for (const item of tempResu) { $('.history-list').append(` < li >${item}</ li > `) } } } // 历史记录点击搜索 $('.history-list').on('click', 'li', function () { let searchVal = $(this).html() $('.search-val').val(searchVal) $('.list ul').css('display', 'block') searchData() }) </ script > </ body > </ html > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通