function ys()
{
         var chkother=document.getElementsByTagName("input"); 
   for (var i=0;i<chkother.length;i++)  {     if( chkother[i].type =="text")   
      
      chkother[i].Attributes.Add("onfocus","alert('天下');")
}
}
<body  onload ="ys()">chkother[i].Attributes.Add("onfocus","alert('天下');")
这句话问题在哪,为什么提示说Attributes为空或不是对象。我想知道是为什么不行?

解决方案 »

  1.   

    chkother[i].setAttribute("onfocus","alert('天下');")
      

  2.   

    我试了下 chkother.lengthd的值总是 0 啊!
      

  3.   

    Attributes --》attributes另外attributes没有Add方法
      

  4.   

    要改成
    chkother[i].setAttribute("onfocus",function (){alert('天下');})
      

  5.   

    chkother[i].setAttribute("onfocus",function (){alert('天下');})这种方法可以
    chkother[i].setAttribute("onfocus","alert('天下');") 这个不行。。不知道为什么。。