html移动应用 input 标签 清除按钮功能如何实现(不触发键盘)

有个需求是:输入框有文本的时候就显示清除按钮,没有文本则隐藏清除按钮,点击清除按钮不能影响键盘弹出的状态。

网上有css实现自动显示和隐藏清除按钮的方案,但是考虑到兼容性,我们还是使用js来实现。

 

css


body{
             background: #eee;
         }
         form{
             margin: 30px 0;
             position: relative;
         }
         #keyword{
             height: 90px;
             font-size: 60px;
             line-height: 90px;
             width: 300px;
         }
         #clear{
             color: red;
             width: 90px;
             height: 90px;
             line-height: 90px;
             text-align: center;
             position: absolute;
             top: 0;
             left: 210px;
             visibility:hidden;
         }


html


<form οnsubmit="return false;" >
     <input type="button" id="clear" value="clear">
     <input type="search" id="keyword">
</form>


js


var keyword = document.getElementById('keyword'),
         clear=document.getElementById('clear'),
         autoShow=function () {
         clear.style.display=keyword.value.length>0?'block':'none';
         clear.style.visibility=keyword.value.length>0?'visible':'hidden';
     };

    keyword.οninput=autoShow;
  

    clear.οnmοusedοwn=function (e) {
         keyword.value = '';
         autoShow();
         keyword.focus();
         e.preventDefault();
         e.stopPropagation();
         return false;
     };


转载于:https://www.cnblogs.com/johnv/p/10075820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值