作移動平臺的應用,使用iscroll使屏幕上下滑動。發現當使用iscroll後,input等不能輸入內容了。只要在iscroll.js文件中加入以下代碼就ok了。node
function allowFormsInIscroll(){ [].slice.call(document.querySelectorAll('input, select, button')).forEach(function(el){ el.addEventListener(('ontouchstart' in window)?'touchstart':'mousedown', function(e){ e.stopPropagation(); }) }) } document.addEventListener('DOMContentLoaded', allowFormsInIscroll, false);
問題緣由是:iscroll須要一直監聽用戶的touch操做,以便靈敏的作出對應效果,因此它把其他的默認事件屏蔽了。瀏覽器
以上代碼原理是:頁面加載完成後查找到全部的'input, select, button'元素並依次綁定'touchstart'或'mousedown'事件,在執行事件的時候中止事件的傳播,這樣行了。code
使用了iscroll以後,你會發現點擊輸入框時不靈敏,常常沒法聚焦;頁面文字也沒法選擇和複製。這是因爲iscroll要監聽鼠標事件和觸摸事件來進行滾動,因此禁止了瀏覽器的默認行爲。
iscroll不分青紅皁白,禁止了瀏覽器的一切默認行爲,致使上述問題。因此咱們須要稍做修改:orm
onBeforeScrollStart: function (e) { var target = e.target; while (target.nodeType != 1) target = target.parentNode; if (target.tagName != ‘SELECT’ && target.tagName != ‘INPUT’ && target.tagName != ‘TEXTAREA’) e.preventDefault(); },