0

在进行 ajax 调用 .loadUrl() 之后,我需要隐藏一个 div (this.elmWheel)。

使用此代码我无法隐藏 div。我在这里做错了什么?我正在使用 jquery 1.4.2

var Viewer = function(url) {
        var scope = this;
        this.elm = '#viewer';
        this.elmWheel = '#loader-wheel';
        this.url = url;
        this.init = function() {
            this.loadWheelInit();
            this.loadUrl();
        };
        this.loadWheelInit = function() {
            $('<div id="' + scope.elmWheel + '">Loading ...</div>').appendTo(this.elm);
        };
        this.loadWheelHide = function() {
            $(this.elmWheel).hide();
            console.log('hide');
        };
        this.loadUrl = function() {
            // simulate loading
            setTimeout(function() {
                // fetch img from api
                $.get(this.url, function(data) {
                    scope.loadWheelHide();
                    console.log('show image');
                    // add img to the dom
                    var img = $('<img id="img">');
                    img.attr('src', this.url);
                    img.appendTo(scope.elm);


                });
            }, 2000);
        };
    };



        <div id="viewer" class="">

        </div>  

我正在使用此代码创建一个实例,正确附加了一个 Loadind 轮,只是无法隐藏它

    var viewer = new Viewer('img/1.jpg');
    viewer.init();
4

1 回答 1

1

然后你正在创建一个加载轮,它得到一个错误的 ID。

this.loadWheelInit = function() {
    $('<div id="' + scope.elmWheel + '">Loading ...</div>').appendTo(this.elm);
};

这导致

<div id="#loader-wheel">Loading...</div>

在一种loadWheelHide方法中,您试图通过 selector 访问负载轮#loader-wheel,但没有这样的 ID。

您需要在 elmWheel 中存储一个 ID

this.elmWheel = 'loader-wheel'

并在搜索时添加一个井号

this.loadWheelHide = function() {
    $('#' + this.elmWheel).hide();
    console.log('hide');
};
于 2013-07-08T07:20:36.597 回答