Javascript 如何在jQuery中将一个div追加到另一个div后隐藏它?

Javascript 如何在jQuery中将一个div追加到另一个div后隐藏它?,javascript,jquery,Javascript,Jquery,在进行ajax调用后,我需要隐藏一个div(this.elmWheel)。loadUrl() 使用此代码,我无法隐藏div。 我做错了什么? 我正在使用jquery 1.4.2 var Viewer = function(url) { var scope = this; this.elm = '#viewer'; this.elmWheel = '#loader-wheel'; this.url = url; thi

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

使用此代码,我无法隐藏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>  

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

this.loadWheelInit = function() {
    $('<div id="' + scope.elmWheel + '">Loading ...</div>').appendTo(this.elm);
};
并在搜索时预先加上一个哈希符号

this.loadWheelHide = function() {
    $('#' + this.elmWheel).hide();
    console.log('hide');
};

如何创建
查看器
实例?它的方法是如何调用的?var viewer=newviewer('img/1.jpg');viewer.init()
#loader wheel
是一个CSS选择器,意思是“获取一个ID等于loader wheel的元素”。还有许多其他CSS选择器,如“.”,用于搜索具有特定CSS类的元素(
$('.myClazz')
以匹配
),或仅使用标记名搜索指定的标记(
$('p')
以匹配
某些文本

)。因此,创建一个元素时,不需要传递选择器,但需要进行搜索。
<div id="#loader-wheel">Loading...</div>
this.elmWheel = 'loader-wheel'
this.loadWheelHide = function() {
    $('#' + this.elmWheel).hide();
    console.log('hide');
};