var loadhandler = function(fun){ if(typeof fun !== "function") return false; if(!!window.onload){ var oldfun = window.onload; window.onload = function(){ oldfun(); fun(); } }else{ window.onload = fun; } } var inputhandler = function(node,fun){ if("oninput" in node){ node.oninput = fun; }else{ node.onpropertychange = fun; } }
//inputhandler实例
<input type="text" name="text" id="text" /> <input type="text" name="text" /> <input type="text" name="text" /> <div id="main"></div>
var text = document.getElementsByName("text"); for(var i=0,j=text.length;i<j;i++){ inputhander(text[i],function(){ var value = 0; for(var ini=0;ini<j;ini++){ value += +text[ini].value; } document.getElementById("main").innerHTML = value; });