Jquery/Ajax问题-将一页上的div内容加载到主页上的div中

发布于 2024-11-06 10:23:07 字数 1112 浏览 0 评论 0原文

我真的希望你能在这里帮助我。我正在制作一个 WordPress 网站,有 6 个页面+主页。这 6 个页面的内容包含在名为 #container 的 div 中。内容包括图像幻灯片等,所有这些都使用各种代码来正确操作和运行。

在首页上,我有一个基于图像的导航菜单,我已通过 html 叶子将其插入到标题内的小部件中。代码如下:

<ul id="navlist">
   <li id="webdesign"><a href="http://mysite.com/web-design/"></a></li>
   <li id="branding"><a href="http://mysite.com/branding/"></a></li>
   <li id="architecture"><a href="http://mysite.com/architecture/"></a></li>
   <li id="writing"><a href="http://mysite.com/writing/"></a></li>
   <li id="blog"><a href="http://mysite.com/blog/"></a></li>
   <li id="contact"><a href="http://mysite.com/contact/"></a></li>
</ul>

我想要做的是,每次单击这 6 个链接之一时,我只想将 div #container 的内容从该特定页面加载到 div #container 上,而不是加载整个页面。主页,或者甚至只是加载并保留该 div 中的所有丰富内容。

当谈到 jquery/ajax 时,我是一个完全的新手,并且已经阅读并尝试了一百万个不同的代码片段,其中一些代码半工作,而大多数根本不起作用。我不知道这是否相关,但是网站页面上的 #container div 包含许多其他 div,所有这些 div 都包含不同类型的动态内容。

如果有人能帮助我,我将不胜感激!

谢谢

埃莉诺

I really hope you can help me out here. I'm making a wordpress site, with 6 pages + the home page. The 6 pages have content contained within a div called #container. The content includes things like image slideshows etc, that all use various code to operate and function correctly.

On the front page, I have an image-based navigation menu, which I have inserted into a widget within the header via an html leaf. Here's the code:

<ul id="navlist">
   <li id="webdesign"><a href="http://mysite.com/web-design/"></a></li>
   <li id="branding"><a href="http://mysite.com/branding/"></a></li>
   <li id="architecture"><a href="http://mysite.com/architecture/"></a></li>
   <li id="writing"><a href="http://mysite.com/writing/"></a></li>
   <li id="blog"><a href="http://mysite.com/blog/"></a></li>
   <li id="contact"><a href="http://mysite.com/contact/"></a></li>
</ul>

What I want to do is every time one of these 6 links is clicked, rather than loading the entire page, I just want to load the contents of the div #container from that particular page into the div #container on the home page, or even just load the and also keep all the rich content within that div doing its thing.

I am a total newbie when it comes to jquery/ajax, and have read and tried out a million different snippets of code, some of which half-work, and most of which don't work at all. I don't know if this is relevant, but the #container div on the site pages contain a lot of other divs, all holding different types of dynamic content.

If anyone can help me, I'll be extremely grateful!!!

Thanks

Eleanor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-11-13 10:23:07

你可以尝试做这样的事情。尽管没有看到它,但我不知道这与其他页面上的内容的配合效果如何

    $(function () {

        //Stop caching if page is likely to change often
        $.ajaxSetup({
            cache: false
        });

        $('#navlist a').click(function (evt) {
           //Grab url from link
           var url = $(this).attr('href'); 
           //This will load the entire page but only select the #container 
           $('#container').load(url +' #container'); 
           evt.preventDefault(); //Stop yourself from navigating to that page
        });

    });

   <ul id="navlist">
      <li id="webdesign"><a href="Secondary.aspx">webdesign</a></li>
   </ul>

   <div id="container"></div>

You could try to do something like this. I don't know how well this will work with the content on the other pages though without seeing it

    $(function () {

        //Stop caching if page is likely to change often
        $.ajaxSetup({
            cache: false
        });

        $('#navlist a').click(function (evt) {
           //Grab url from link
           var url = $(this).attr('href'); 
           //This will load the entire page but only select the #container 
           $('#container').load(url +' #container'); 
           evt.preventDefault(); //Stop yourself from navigating to that page
        });

    });

   <ul id="navlist">
      <li id="webdesign"><a href="Secondary.aspx">webdesign</a></li>
   </ul>

   <div id="container"></div>
放肆 2024-11-13 10:23:07

@eleanonr,你总是可以为你的元素有一个占位符,最后说 .html

例如,一旦 ajax 完成,你就有一个用于 ajax 结果的占位符

<div "ajaxResults"> </div>

,你可以定位该 div 并绘制它,这样它就不会刷新完整的页面。

$.ajax({
  url: "test.html"
  success: function(results){
   $("#ajaxResults").html(results);
  }
});

如果您需要任何进一步的帮助,请告诉我

@eleanonr , you can always have a place holder for your elements and say .html finally

for example you have a place holder for ajax results

<div "ajaxResults"> </div>

once the ajax is completed you can target that div and paint it , so that it won't refresh the complete page.

$.ajax({
  url: "test.html"
  success: function(results){
   $("#ajaxResults").html(results);
  }
});

please let me know if you want any furthur help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文