日期:2014-05-16  浏览次数:20381 次

我的页面中有很多<input type="text">标签,这些标签我均要设置onclick,onchange等事件函数,我该怎么设置?
我不想写
<input   type= "text "   onclick= "f() "   onchange= "f1() ">
我想能不能在javascript直接指定所有 <input   type= "text "> 标签的onclick,onchange事件函数都指一个函数,
有办法吗?

------解决方案--------------------
var ipts = document.getElementsByTagName( "input ");
for(var i=0,j=ipts.lenth;i <j;i++)
if(ipts[i].type== "text "){
ipts[i].onclick=function(){};
// ipts.attachEvent( "onclick ",fun);
//ipts.addEventListener( "click ",fun,true|false);
ipts[i].onchange = function(){};
}
------解决方案--------------------
<input type= "text " id= "input1 " class= "txt "/>
<input type= "text " id= "input2 " class= "txt "/>
<script>
window.onload = function(){
var is = document.getElementsByTagName( "input ");
for (var i=0;i <is.length;i++){
if (is[i].className == "txt "){
is[i].onclick = click;
is[i].onchange = chg;
}
}
}
function gelm(){
if(window.event)return window.event.srcElement;
var f=arguments.callee;
while(caller=f.caller){
f=f.caller
}
return f.arguments[0].target;
}
function click(){
var obj = gelm();
alert(obj.id);
}
function chg(){
var obj = gelm();
alert(obj.value);
}
</script>