0

在对象构造函数中有一个名为addToViewport()的方法,它的作用是在预加载图像后简单地显示图像:

window.onload = function(){

            function ViewportObject(){
                this.src = "https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=asdasdasd&choe=UTF-8&chld=L|0";
                this.addToViewport = function(){
                    // Determine the DOM object type to create
                        var DOM_elem = "img",
                            obj = $(document.createElement(DOM_elem)),
                            img = new Object();

                            obj.addClass("object");
                            obj.css({"z-index":"100","width":"300px","height":"auto"});

                            // Preload the image before rendering it and computing its size
                            img.src = this.src;
                            img.onload = function(){

                                obj.attr("src",this.src);
                                obj.appendTo("body");
                            }                       

                }
            }

            var o = new ViewportObject();   
            o.addToViewport();
        }

我遇到的问题是脚本没有进入“onload”事件处理程序块,所以图像没有显示。我在http://picselbocs.com/test/上整理了一个与上述脚本相同的网页,供您实时查看。

我在这里做错了什么?

4

1 回答 1

3

尝试这个:

改变这个

img = new Object();
....
img.src = this.src;
img.onload = function(){

    obj.attr("src",this.src);
    obj.appendTo("body");
} 

img = new Image();
....
img.onload = function(){

    obj.attr("src",this.src);
    obj.appendTo("body");
} 
img.src = this.src;
于 2012-09-24T18:38:48.453 回答