Internet Explorer的innerHTML对象不支持此属性或方法

Internet Explorer innerHTML "Object doesn't support this property or method"

本文关键字:属性 方法 不支持 对象 Explorer innerHTML Internet      更新时间:2023-09-26

我有一个脚本,检查div Id的值。

   <div id="hour" style="display:none;">2</div>
   <div id="min" style="display:none;">1</div>
   <div id="sec" style="display:none;">3</div>
   hour = document.getElementById("hour").innerHTML;
   min = document.getElementById("min").innerHTML;
   sec = document.getElementById("sec").innerHTML;

工作在Chrome,但不是在Internet Explorer(这是我需要它的工作)

它给了我一个错误,"对象不支持这个属性或方法"

有什么更简单的方法(最好是一行)来解决这个问题?

我认为问题是您使用与DOM元素相同的名称命名变量。我好像记得IE把dom元素当作一等公民,所以这可能是你的问题的原因。

试题:

var hourHtml = document.getElementById("hour").innerHTML;
var minHtml = document.getElementById("min").innerHTML;
var secHtml = document.getElementById("sec").innerHTML;

不要使用innerHTML。显然这是微软的专有功能,所以不在DOM标准中…

看看这个问题