Javascript 如何实现这个Ajax脚本?
这是一个脚本,当点击一个链接时,它会从某个地方拖出一个页面 并将其插入当前页面的div中。很简单,是的,但是作为 我的脑袋似乎很笨,我不知道该怎么实现它 i、 e.我如何制定链接,使其将脚本指向页面 我想在我想要的div中加载 剧本:Javascript 如何实现这个Ajax脚本?,javascript,Javascript,这是一个脚本,当点击一个链接时,它会从某个地方拖出一个页面 并将其插入当前页面的div中。很简单,是的,但是作为 我的脑袋似乎很笨,我不知道该怎么实现它 i、 e.我如何制定链接,使其将脚本指向页面 我想在我想要的div中加载 剧本: $(document).ready(function() { // Check for hash value in URL var hash = window.location.hash.substr(1); var href
$(document).ready(function() {
// Check for hash value in URL
var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href.length-5)){
var toLoad = hash+'.html #content';
$('#content').load(toLoad)
}
});
$('#nav li a').click(function(){
var toLoad = $(this).attr('href')+' #content';
$('#content').hide('fast',loadContent);
$('#load').remove();
$('#wrapper').append('<span id="load">LOADING...</span>');
$('#load').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
function loadContent() {
$('#content').load(toLoad,'',showNewContent())
}
function showNewContent() {
$('#content').show('normal',hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
var toLoad = $(this).attr('href')+' #content';
function loadContent() {
$('#content').load(toLoad,'',showNewContent)
}
PS:本教程的所有内容都是。基本上拦截所有链接点击,并发出AJAX请求。。。记住在click回调函数的末尾返回false
$('a').click(function () {
var href = $(this).attr('href');
$.ajax({
url: href,
success: function (res) {
$(res).appendTo('#target'); // add the requested HTML to #target
}
});
return false; // important
});
基本上截获所有链接点击并发出AJAX请求。。。记住在click回调函数的末尾返回false
$('a').click(function () {
var href = $(this).attr('href');
$.ajax({
url: href,
success: function (res) {
$(res).appendTo('#target'); // add the requested HTML to #target
}
});
return false; // important
});