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
| $(function() {
var historyListJson = localStorage.getItem('historyList') || '[]' var historyListArr = JSON.parse(historyListJson) var render = function() { var html = '' historyListArr.forEach(function(item, i) { html += '<li><span>' + item + '</span><a data-index="' + i + '" href="javascript:">删除</a></li>' }) html = html || '<li>没有搜索记录</li>' $('ul').html(html) } render()
$('[type="button"]').on('click', function() { var key = $.trim($('[type=search]').val()) if (!key) { alert('请输入搜索关键字') return false } historyListArr.push(key) localStorage.setItem('historyList', JSON.stringify(historyListArr)) render() $('[type=search]').val('') })
$('ul').on('click', 'a', function() { var index = $(this).data('index') historyListArr.splice(index, 1) localStorage.setItem('historyList', JSON.stringify(historyListArr)) render() }) $('div a').on('click', function() { historyListArr = [] localStorage.setItem('historyList', '') render() }) })
|