如何使函数在ajax调用中调用函数?
问题描述:
如何使这样的:如何使函数在ajax调用中调用函数?
$('.projects').hover(function(){
$defBox.stop(true, true).fadeToggle(1000).html('test');
});
成一个函数,然后调用它在一个Ajax调用?
// Check to ensure that a link with href == hash is on the page
if ($('a[href="' + hash + '"]').length) {
// Load the page.
var toLoad = hash + '.php #main-content';
$('#main-content').load(toLoad);
}
$('nav ul li a').click(function() {
var goingTo = $(this).attr('href');
goingTo = goingTo.substring(goingTo.lastIndexOf('/') + 1);
if (window.location.hash.substring(1) === goingTo) return false;
var toLoad = $(this).attr('href') + ' #main-content',
$content = $('#main-content'),
$loadimg = $('#load');
$content.fadeOut('fast', loadContent);
$loadimg.remove();
$content.append('<span id="load"></span>');
$loadimg.fadeIn('slow');
window.location.hash = goingTo;
function loadContent() {
$content.load(toLoad, '', showNewContent)
}
function showNewContent() {
$content.fadeIn('fast', hideLoader, log);
}
function hideLoader() {
$loadimg.fadeOut('fast');
}
return false;
});
我的功能
$('.projects dl').find('dd').hide();
function fadeBox(){
$('#def-box').stop(true, true)
.fadeToggle(1000)
.html('test');
}
$('.projects').hover(function(){
fadeBox();
});
function descBox(){
$('.projects dl').find('dd').hide();
var $data = $(this)
.next('dd')
.html();
$('#def-box').html($data);
}
$('.projects dl dt').hover(function(){
descBox();
});
和Ajax
function showNewContent() {
fadeBox();
descBox();
$content.fadeIn('fast',hideLoader);
}
而且这是行不通的。它假设当dt
被徘徊时,其隐藏的dd
元素位于它在#def-box
中的位置。
function descBox(){
$('.projects dl').find('dd').hide();
var $data = $(this)
.next('dd')
.html('test');
$('#def-box').html($data);
}
另外,dd
元素,如果我点击anotherpage隐藏,但在负荷... http://example.co/#home
的dd
不是隐藏不动了。
答
// create the function
function myfunc(){
$defBox.stop(true, true).fadeToggle(1000).html('test');
}
// use it as the handler
$('.projects').hover(myfunc);
// call it in the showNewContent function
function showNewContent() {
myfunc();
$content.fadeIn('fast',hideLoader, log);
}
使它成为一个函数很容易,用函数(){`和`}`包围它。但现在确定我明白你的意思,用AJAX调用它 - 在AJAX返回时运行它吗? –
2011-02-12 18:31:53
是的,这是我的意思 – nowayyy 2011-02-12 18:53:33