a{-webkit-tap-highlight-color:transparent; }
本控件适用于各种手机滑动翻页专题,虽然说网上已经有同类控件,而且比较成熟。但个人认为这还是属于比较值得自己造的轮子。也顺手练练 MVVM模式。
这次来说说, 移动开发中限制字符长度的正确姿势
按道理来说限制长度就是通过 onpropertychange 或者 oninput 监听输入内容,
然后多的 用 substr 截取掉就可以了
input.addEventListener('input', function(){ this.value = this.value.substr(0, 8); });
其实这功能 更多是用于 node 服务器接口输出数据的时候使用,因为 node 这边 的接口输出要 utf8编码 来避免 因为编码带来的问题。下面是执行代码
/** * 字符串转 utf8编码 * @param {string} 文本 * @return {string} 转化后文本 */ var utf8Encode = function(str){ if(typeof str != 'string'){ return str; } return str.replace(/[\u4E00-\u9FA5]/g, function($1){ return "\\u" + ("00"+$1.charCodeAt().toString(16)).slice(-4);}); }
var scrolling = { // 禁止滚动 disable: function(){ var she = this; if (window.addEventListener) { window.addEventListener('DOMMouseScroll', she.handle, false); window.addEventListener('touchmove', she.handle, false); window.onmousewheel = document.onmousewheel = she.handle; document.onkeydown = she.keydown; } }, // 恢复滚动 enable: function(){ var she = this; if (window.removeEventListener) { window.removeEventListener('DOMMouseScroll', she.handle, false); window.removeEventListener('touchmove', she.handle, false); } window.onmousewheel = document.onmousewheel = document.onkeydown = null; }, keydown: function(e){ var keys = [37, 38, 39, 40]; for (var i = keys.length; i--;) { if (e.keyCode === keys[i]) { e.preventDefault && preventDefault(e); return; } } }, handle: function(e){ e = e || window.event; e.returnValue = false; e.preventDefault && preventDefault(e); } };
如有疑问or建议可通过以下方式跟我取得联系.
Q Q: | 373435871 |
---|---|
Email: | jackness1208@gmail.com |