代码之家  ›  专栏  ›  技术社区  ›  Saleh

如何使用javascript更新Firefox中的DIV内容?

  •  0
  • Saleh  · 技术社区  · 14 年前

    我在javascript中有这段代码,用于在DIV中显示时间并对其进行更新:

      if (!document.all)
        return
    
      var Digital=new Date()
      var hours=Digital.getHours()
      var minutes=Digital.getMinutes()
      var seconds=Digital.getSeconds()
      var dn="AM" 
    
      if (hours>12){
       dn="PM"
       hours=hours-12
      }
    
      if (hours==0)
       hours=12
    
      if (minutes<=9)
       minutes="0"+minutes
    
      if (seconds<=9)
       seconds="0"+seconds
       var ctime=hours+":"+minutes+":"+seconds
       $("#c").text(ctime);
    
       setTimeout("show2()",1000)
    }
    
    window.onload=show2
    

    我有这个分区:

    <div id=""c></div>
    

    在IE中,一切都正常,但当我在Firefox中尝试时,它就不起作用了。

    我也试着用

    c.innerHTML=ctime
    

    但它不起作用。

    在IE和火狐上,正确的方法是什么?

    谢谢

    3 回复  |  直到 14 年前
        1
  •  3
  •   fcalderan    14 年前

    这是第一行

    if (!document.all)
        return
    

    自。所有属性仅存在于Explorer和Opera上。

    您正在阻止其他浏览器继续执行代码

        2
  •  2
  •   Fosco    14 年前

    尝试 <div id="c"></div> document.getElementById('c').innerHTML = ctime;

    摆脱 if (!document.all) return

        3
  •  1
  •   Onkelborg    14 年前

    去除

      if (!document.all)
        return
    

    因为document.all只是一个IE扩展,而firefox不支持它。