简体   繁体   English

AJAX加载内容无页面刷新

[英]AJAX Load Content No Page Refresh

I've been beating my brain trying to figure out why this won't work. 我一直在绞尽脑汁想弄清楚为什么这行不通。 The initial page loaded content does load up just fine, but clicking the menu links does nothing at all. 初始页面加载的内容确实可以加载,但是单击菜单链接根本没有任何作用。 It won't load the pages. 它不会加载页面。 Hovering over the menu links shows the correct content url, but clicking them does nothing. 将鼠标悬停在菜单链接上会显示正确的内容URL,但是单击它们不会执行任何操作。 Here is the test site. 这是测试站点。 menu link 1 and menu link 2 are the test links. 菜单链接1和菜单链接2是测试链接。 The content will load up in the main content div: brandonbutler.com/alex/ 内容将在主要内容div中加载:brandonbutler.com/alex/

I got the code from this tutorial . 我从本教程中获得了代码。 Here is the code I'm using: 这是我正在使用的代码:

$(document).ready(function() {

// initial page load
$('#main').load('toc-content/landing.html');

// handle menu click
$('ul#keepcolors li a').click(function(){
    var page = $(this).attr('href');
    $('#main').load('toc-content/'+ page +'.html');
    return false;
});

});

I wen to your site and saw this: 我浏览了您的网站并看到了以下内容:

<a href="toc-content/toc1.html">menu 1</a>

That means that doing 这意味着

var page = $(this).attr('href');
$('#main').load('toc-content/'+ page +'.html');

Will result on page being "toc-content/toc1.html" and you trying to load this: "toc-content/toc-content/toc1.html.html" 将在页面上显示“ toc-content / toc1.html”,而您尝试加载此页面:“ toc-content / toc-content / toc1.html.html”

Change to this instead: 改为此:

 var page = $(this).attr('href');
 $('#main').load(page);

Hope it helps! 希望能帮助到你!

Google around for 'asynchronous'. 谷歌周围的“异步”。

The issue is that the page is loading after $('ul#keepcolors... ').click(..) executes so it's not binding the click to any element because those elements don't exist on the page until $('main').load(..) is complete. 问题是页面在$('ul#keepcolors ...').click(..)执行后正在加载,因此它不会将click绑定到任何元素,因为这些元素直到$('才在页面上不存在main')。load(..)已完成。

Try this: 尝试这个:

$('#main').load('toc-content/landing.html', function () {
   // handle menu click
   $('ul#keepcolors li a').click(function(){
      var page = $(this).attr('href');
      $('#main').load('toc-content/'+ page +'.html');
      return false;
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM