+ -
当前位置:首页 → 问答吧 → 这个JS在IE有错误提示,请大侠指点

这个JS在IE有错误提示,请大侠指点

时间:2010-05-05

来源:互联网

复制内容到剪贴板
代码:
           var showAD = {
                curve: function(t, b, c, d, s) {
                    if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
                    return c / 2 * ((t -= 2) * t * t + 2) + b
                },
                fx: function(from, to, playTime, onEnd) {
                    var Me = this,
                    who = this.adWrap,
                    position = 0,
                    changeVal = to - from,
                    curve = this.curve;
                    playTime = playTime / 10;
                    who.style.overflow = 'hidden';
                    function _run() {
                        if (position++<playTime) {
                            who.style.height = Math.max(1, Math.abs(Math.ceil(curve(position, from, changeVal, playTime)))) + "px";
                            setTimeout(_run, 10)
                        } else {
                            onEnd && onEnd.call(Me, to)
                        }
                    };
                    _run()
                },
                init: function(info) {
                    var Me = this,
                    loadImg = new Image;
                    loadImg.src = info.endImgURL;
                    window.onload=function() {
                        Me.endImgURL = info.endImgURL;
                        Me.img = document.getElementById(info.imgID);
                        Me.adWrap = document.getElementById(info.adWrapID);
                        var max = Me.img.height;
                        setTimeout(function() {
                            Me.fx(max, 0, 500,
                            function(x) {
                                this.img.src = this.endImgURL;
                                this.curve = function(t, b, c, d) {
                                    if ((t /= d) < (1 / 2.75)) {
                                        return c * (7.5625 * t * t) + b
                                    } else if (t < (2 / 2.75)) {
                                        return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b
                                    } else if (t < (2.5 / 2.75)) {
                                        return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b
                                    } else {
                                        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b
                                    }
                                };
                                this.fx(0, this.img.height,600)
                            })
                        },
                        info.timeout || 1000)
                    };
                }
            };
            showAD.init({
                adWrapID: 'ad3',
                imgID: 'adImg',
                endImgURL: '/res/inc/index/images/950X80_04011.jpg'
            });
在IE下的的错误提示,请大侠指点

消息: 缺少对象
行: 32
字符: 25
代码: 0

[ 本帖最后由 huacker 于 2010-5-5 17:55 编辑 ]

附件

0505mall.rar (82.84 KB)

2010-5-5 17:55, 下载次数: 2

作者: huacker   发布时间: 2010-05-05

把整个页面一起帖出来 才好处理啊!

作者: szpoppy   发布时间: 2010-05-05

引用:
原帖由 szpoppy 于 2010-5-5 17:50 发表
把整个页面一起帖出来 才好处理啊!
你好,已将图片和代码贴了,谢谢

作者: huacker   发布时间: 2010-05-05

是不是有ID没定义?

作者: ilove608   发布时间: 2010-05-05

哪个ID?

作者: huacker   发布时间: 2010-05-05

问题解决思路:
1.
浏览器提示 index_ad.js 文件第32行错误.
打开 index_ad.js 文件,找到第32行,代码如下:
复制内容到剪贴板
代码:
var max = Me.img.height;
错误提示的内容是: 缺少对象,在这出错的原因是:
Me.img 的值为 null,
所以再往下取,也就是 null.height
而 null 没有 height 所以浏览器报错,提示 缺少对象

2.
既然是 Me.img 为 null,那就要看这个值是哪来的.
往上看到第30行,代码如下:
复制内容到剪贴板
代码:
Me.img = document.getElementById(info.imgID);
Me.img是使用 document.getElementById 获取  info 对象的 imgID 值取的DOM对象.

3.找 info 的是哪来的.
第 24 行:
复制内容到剪贴板
代码:
init: function(info) {
可以看到是 showAD 对象的 init 方法 被调用时传递的.

然后找  showAD.init 调用的地方.

第55到59行:
复制内容到剪贴板
代码:
showAD.init({
    adWrapID: 'ad3',
    imgID: 'adImg',
    endImgURL: '/res/inc/index/images/950X80_04011.jpg'
});
分析出 info.imgID的值为 adImg,
然后在 index.html 文件中 搜索 adImg 这个字符串,未搜索到.

问题找到, 因为 页面中不存在 ID为adImg的HTML对象,造成
Me.img的值为 null
而下面的代码在使用的时候没有判断它的值,而直接使用 Me.img.height 而产生的错误.

解决办法:
在页面中添加 ID为adImg的HTML标签.(具体是个什么标签,这俺就不清楚了 )

作者: faeng220   发布时间: 2010-05-05

哦,谢谢,受教了

作者: huacker   发布时间: 2010-05-05