当我想更改元素的内容时,它可以在没有效果的情况下正常工作,并且可以通过效果执行此行为:
- 将内容更改为新内容
- 对新内容有隐藏效果
- 展示效果如何,还是新内容
这是每次我需要更改内容时启动的代码:
function changePage(slug) {
var content = $('#content');
var title = $('#name');
$.getJSON('http://'+location.host+'/'+slug+'.json', function(data) {
$('#main').fadeOut('slow');
title.html(data.name);
content.html(data.content);
$('#main').fadeIn('slow');
});
}
以下是运行此函数的代码:
var app = new Sammy.Application(function() {with(this){
get('#/', function() { with(this) {
changePage('_home');
}});
get('#/:page', function() { with(this) {
changePage(params['page']);
}});
}});
$(function(){app.run();});
我是萨米,http://github.com/quirkey/sammy/ .
预先感谢您的帮助!
最佳答案
fadeOut 和 fadeIn 函数有一个完整的处理程序,您需要使用例如:
$.getJSON('http://'+location.host+'/'+slug+'.json', function(data) {
$('#main').fadeOut('slow',function(){
title.html(data.name);
content.html(data.content);
$('#main').fadeIn('slow');
});
});
现在,一旦淡出效果完成,它将执行数据切换并淡入!
安德鲁
关于javascript - 如何防止 jQuery 在隐藏元素之前加载修改后的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1252551/