Jquery:如何做attr后的回调? [英] Jquery: how to do a callback after attr?
本文介绍了Jquery:如何做attr后的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想先更改img src,然后完成其他。类似这样的东西 -
$('#bgImage')。attr('src','images /'+ bgImage,function (){
alert('inside');
});
我将如何做?
解决方案
另一行代码可能?
$('#bgImage')。attr 'src','images /'+ bgImage),
$('#searchPin')。css(top,y +px);
$('#searchPin')。css(left,x +px);
如果你想等待图片加载,你可能正在寻找< href =http://api.jquery.com/load-event/> 载入
:
$('#bgImage')。load(function(){
$('#searchPin')。css(top,y +px);
$('#searchPin')。css(left,x +px);
})attr('src','images /'+ bgImage)
注意 load(...)
事件处理程序在使用 attr
更改 src
属性之前创建>
如果您不止一次这样做,您可能需要查看 unbind
。
I want to change the img src at first then accomplish others. Something like this-
$('#bgImage').attr('src','images/'+bgImage, function() {
alert('inside');
});
how will i do that?
解决方案
Another line of code perhaps?
$('#bgImage').attr('src','images/'+bgImage),
$('#searchPin').css("top",y+"px");
$('#searchPin').css("left",x+"px");
If you want to wait for the image to load, you're probably looking for the event load
:
$('#bgImage').load(function() {
$('#searchPin').css("top",y+"px");
$('#searchPin').css("left",x+"px");
}).attr('src','images/'+bgImage);
Note that the load(...)
event handler was created before changing the src
attribute using attr
- in case the image has been cached.
If you're doing this more than once you might want to look into unbind
too.
这篇关于Jquery:如何做attr后的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文