[英]How to hide a div after being appended to another div in jQuery?
I need to hide a div (this.elmWheel) after an ajax call is made .loadUrl(). 我需要在进行ajax调用.loadUrl()之后隐藏div(this.elmWheel)。
Using this code I am not able to hide the div. 使用此代码,我无法隐藏div。 What am I doing wrong here?
我在这里做错了什么? I am using jquery 1.4.2
我正在使用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>
I am creating an instance with this code, image an Loadind wheel are appended correctly, just not able to hide it 我正在使用此代码创建一个实例,正确加载了Loadind轮的图像,只是无法将其隐藏
var viewer = new Viewer('img/1.jpg');
viewer.init();
Then you are creating a loading wheel, it gets a wrong ID. 然后,您正在创建一个装载轮,它得到了错误的ID。
this.loadWheelInit = function() {
$('<div id="' + scope.elmWheel + '">Loading ...</div>').appendTo(this.elm);
};
This results in 这导致
<div id="#loader-wheel">Loading...</div>
In a loadWheelHide
method, you are trying to access load wheel by selector #loader-wheel
, but there are not such ID. 在
loadWheelHide
方法中,您尝试通过选择器#loader-wheel
访问负载#loader-wheel
,但没有这样的ID。
You need to store an ID in elmWheel 您需要将ID存储在elmWheel中
this.elmWheel = 'loader-wheel'
And prepend a hash sign when you do searching 并在搜索时添加一个井号
this.loadWheelHide = function() {
$('#' + this.elmWheel).hide();
console.log('hide');
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.