js text输入框输入掩码实现
html的text输入框如何实现输入掩码,类似password输入框输入效果。
js text输入框输入掩码实现源代码如下
<!doctype html> <title>js text输入框输入掩码实现</title> <style> div.dvinput{display:inline-block;position:relative} div.dvinput input{border:solid 1px #000;height:20px;line-height:20px;display:block;background:#fff;padding:0;} div.dvinput div{position:absolute;background:#fff;left:1px;top:1px;line-height:20px;} </style> <!--[if lte IE 7]><style>div.dvinput{display:inline;}</style><![endif]--> <script> if (!String.prototype.repeat) String.prototype.repeat = function (num) { var r = ''; for (var i = 0; i < num; i++) r += this; return r; }; function setStar(o, blur) { o.nextSibling.innerHTML = '*'.repeat(o.value.length - (blur ? 0 : 1)); } </script> <div class="dvinput"><input type="text" value="123" onkeyup="setStar(this, true)" onblur="setStar(this)" /><div onclick="this.previousSibling.focus()"></div></div>
小提示:要注意添加xhtml申明编码CSS1Compat,而不是BackCompat模式,要时backCompat模式div的高度需要用js来设置,chrome,firefox下设置input display为block无效,height也无效,网上找设置height无效添加box-sizing:border-box解决高度问题,但是测试backCompat模式也无效。
/* 兼容WebKit核心和IE核心的浏览器,经测试backCompat模式无效 */ input { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; height: 150px }
加支付宝好友偷能量挖...
原创文章,转载请注明出处:js text输入框输入掩码实现