实现长按删除功能
搜索结果页的历史记录需要实现批量删除和单个长按出现删除按钮,然后删除。注意点:系统默认有长按出现复制粘贴。在开始之前需要取消默认行为
*{ -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none; }
<a class="current" href="www.baidu.com">百度</a> <a class="current" href="www.sougou.com">搜狗</a>
css
body{ margin:0; padding:0; } a{ list-style: none; text-decoration: none; margin:20px; display:inline-block; } .current{ color:#333; padding:4px 10px; border-radius:6px; background:#ccc; text-align:center; } .delbtn{ padding-right:20px; background:#ccc url(images/cha.png) no-repeat right center; background-size:1.2rem; }
js
var timeout; $(function(){ $(".current").mousedown(function(){ timeout=setTimeout(()=>{ //es6写法解决this指向问题 $(this).siblings('a').removeClass('delbtn'); //其他元素移除删除图标,当前元素添加删除图标 $(this).addClass('delbtn'); var orgin=$(this).attr('href'); //移花接木 避免href跳转问题 $(this).attr('data-href',orgin); $(this).attr('href','javascript:void(0)'); // 添加删除事件 $(this).attr('onclick','delSingle(this)'); //出现删除图标之后,绑定单击事件
},1000) });
$(".current").mouseup(function(){
clearTimeout(timeout);
});
})
function delSingle(obj){
if($(obj).hasClass('delbtn')){
$(obj).hide() }
}
浙公网安备 33010602011771号