【问题标题】:How to update DIV content in Firefox using Javascript?如何使用 Javascript 更新 Firefox 中的 DIV 内容?
【发布时间】:2010-10-30 01:02:11
【问题描述】:

我在 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:

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

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

我也试过

c.innerHTML=ctime

它不工作。

在 IE 和 Firefox 上的正确方法是什么?

谢谢

【问题讨论】:

  • 明显的问题,但你确实启用了 JS 对吗?

标签: javascript jquery html firefox


【解决方案1】:

这是第一行

if (!document.all)
    return

因为 .all 属性仅存在于 Explorer 和 Opera 上。

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

【讨论】:

    【解决方案2】:

    试试&lt;div id="c"&gt;&lt;/div&gt;document.getElementById('c').innerHTML = ctime;

    并摆脱if (!document.all) return

    【讨论】:

    • 如果 $("#c") 是 JQuery,我想。它解析为 document.getElementById('c')。
    • 它被标记为 jQuery,所以应该是 jQuery :)
    • 顺便说一句,innerHTML 比设置文本更危险,因为 innerHTML 不会转义任何东西
    【解决方案3】:

    删除

      if (!document.all)
        return
    

    因为 document.all 是一个仅限 IE 的扩展,而 FireFox 不支持它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2018-04-05
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2017-05-21
      相关资源
      最近更新 更多