1

我继承了一个带有一些时髦代码的网站。在 IE 10 开始抛出此错误之前,它一直保持良好状态:

Unable to get property 'offsetHeight' of undefined or null reference Line: 27 Char: 1 Code: 0 UR 

这是代码:

<script type="text/javascript">
function transDiv(){
var theheight=document.getElementById('transcontent').offsetHeight;
document.getElementById('translayer').style.height=theheight+'px';
}
window.onload=transDiv;
setInterval("transDiv()",1); //this handles text resizing through the users browser
</script>

任何想法/帮助将不胜感激。似乎在 Chrome 和 FF 以及客户端的 IE 版本(8,呃)中工作得很好。

4

1 回答 1

1

元素没有时间在 1 毫秒间隔内加载。您可以将代码更改为此作为一种解决方法,以保持完全相同的功能:

function transDiv() {
    var content = document.getElementById('transcontent');
    var layer = document.getElementById('translayer');
    if (!content || !layer)
        return;
    var theheight = content.offsetHeight;
    layer.style.height=theheight+'px';
    window.setTimeout(transDiv, 1);
}
window.onload = transDiv;

然而,由于它似乎是为了处理调整大小,所以只需捕获事件而不是使用计时器。摆脱该window.setTimeout()行并将其添加到代码下方:

window.onresize = transDiv;
于 2013-09-12T14:37:35.367 回答