.load之后,div不会改变高度,但第二秒之后它会改变
让我们直接进入代码:
$(".btn-slide").live("click", function(e){
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
//deo za dobavljanje odgovarajuce stranice
link = $(this).attr("href");
if($('#post-content').hasClass("active")){
$("#post-content").slideToggle("slow");
$('#post-content').toggleClass("active");
}
$('#post-content').load(link);
$("#post-content").slideToggle("slow");
$('#post-content').toggleClass("active");
$('#content').height($('#post-content').height()); return false;
});
因此,我通过单击将一些内容加载到当前页面/div 中,但内容 div 在第一次调用后不会调整大小。但是当我再次单击该链接时,它会调整大小...... 有人解决吗?
Let's get straight to the code:
$(".btn-slide").live("click", function(e){
if (e.preventDefault) {
e.preventDefault();
}
else {
e.returnValue = false;
}
//deo za dobavljanje odgovarajuce stranice
link = $(this).attr("href");
if($('#post-content').hasClass("active")){
$("#post-content").slideToggle("slow");
$('#post-content').toggleClass("active");
}
$('#post-content').load(link);
$("#post-content").slideToggle("slow");
$('#post-content').toggleClass("active");
$('#content').height($('#post-content').height()); return false;
});
So, I'm loading some contents into my current page/div on a click, but the content div just won't resize after the first call. But when I click the link again, it resizes...
Solution anybody?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的具体问题尚不清楚,但问题的原因很清楚。这与活跃阶级有关。因为我们看不到活动类是什么——我不能肯定地说。但让我们看看会发生什么:
基于此
如果 active 启动,则第一次运行后不会发生任何变化。
如果活动开始,则案例 2 运行一次,案例 1 从此开始运行。
听起来您想确保案例 1 是唯一运行的事情,确保 #post-content 开始上课活跃,你应该没问题。
另一方面,您的代码没有多大意义,因此您可能想要更改它、添加注释、重构等。
Your exact problem is not clear, but the cause of your problem is clear. It has to do with the active class. Since we can't see what the active class is -- I can't say for sure. But lets look at what happens:
Based on this
If active starts on there is no change after the first run.
If active starts off, then case 2 gets run once and case 1 gets run from then on.
Sounds like you want to make sure case 1 is the only thing run make sure #post-content starts having class active and you should be fine.
On the other hand, your code does not make so much sense, so you might want to change it, add comments, re-factor, etc.
问题是 $('#post-content').height() 是未知的,直到 SlideToggle 动画完成。因此,只需将调整大小逻辑放入 SlideToggle 的回调函数中即可解决该问题。这是对我有用的代码:
谢谢大家的评论。我会学习更好地组织我的代码^_^
The problem was that the $('#post-content').height() is unknown until the slideToggle animation finishes. So just putting the resize logic inside a callback function of the slideToggle solved that. Here is the code that worked for me:
Thank you all for your comments. I will learn to better organize my code ^_^