我需要在ajax調用.loadUrl()後隱藏div(this.elmWheel)。如何在jQuery中追加另一個div後隱藏div?
使用此代碼我無法隱藏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();
你如何創建一個'Viewer'實例?如何調用方法? – Olegas
var viewer = new Viewer('img/1.jpg'); viewer.init(); – GibboK