【问题标题】:Wordpress, ajax loaded contentWordpress,ajax加载的内容
【发布时间】:2017-11-12 00:38:22
【问题描述】:

所以,我非常喜欢 php、ajax 和任何 javascript。我已经通过各种教程和陈腐的方法将这个网站放在一起,使事情顺利进行。但是,我遇到了两个围绕 URL 的问题。

  1. 我不知道如何使用后退按钮将某些内容正确绑定到点击帖子时发生的操作。

  2. 子网址直接指向帖子内容(single.php),而不是内容拉起的索引模板。

现在,这是我的代码。我敢肯定它有一百万个问题。

(function($) {

  $(function($) {
        $(".post-link").click(function loadContent(){
            $(this).parent().addClass('fullscreen');
            $('.close').toggleClass('closeactive').removeClass('close');
            $(".back, .next").show();
            $('body').addClass('noscroll');

        });

        $(document).ready(function(){
        $.ajaxSetup({cache:false});
        $(".post-link").click(function(e){
            var post_link = $(this).attr("href");

            $(this).siblings('#single-post-container').html("content loading");
            $(this).siblings('#single-post-container').load(post_link);
            window.history.pushState('object', 'New Title', post_link);
            e.preventDefault();

        window.onpopstate = function(event) {
            $("#loading").show();
            console.log("pathname: "+location.pathname);
            loadContent(location.pathname);
            $(this).parent().removeClass('fullscreen');
        };

        return false;
        });

    });
});

基本上,发生的事情是大量的 css 和 ajax 调用。我不完全确定我做对了,但它运行得很好。你可以在我的网站 www.andbloom.com 上看到它的实现。任何帮助将不胜感激!

【问题讨论】:

  • 这是一个糟糕的网站!我希望我能提供帮助,也不是最适合 ajax 和 js 的。我不太喜欢 ajaxy 网站,但我真的很喜欢这个。当 Netflix 更改为 ajax 时,我讨厌它。对于历史记录,请使用 html5 历史记录 (css-tricks.com/using-the-html5-history-api)。您可能想在 codeable.io(示例)上雇用一名忍者,或者在 Twitter 上进行一些易货交易。真的很棒!
  • 还可以在这里查看源代码和 .js,非常简单的版本(旧但仍然值得学习):view-source:demo.tutorialzine.com/2009/09/simple-ajax-website-jquery/…
  • 只是为您的网站添加书签。我是漫威怪胎!非常棒。
  • 哦,谢谢!我已经查看了很多不同的 ajax 加载内容示例,我的困难在于我有围绕 url 更改的操作。

标签: php jquery ajax wordpress url


【解决方案1】:

第一点

你已经绑定了 back 事件,用

window.onpopstate = function(event) {
    $("#loading").show();
    console.log("pathname: "+location.pathname);
    loadContent(location.pathname);
    $(this).parent().removeClass('fullscreen');
};

这个函数运行的时候报错了,因为loadContent函数没有被定义(需要从click()中取出来,如果要使用之前定义好了)。
另一件事是您不应该在$(".post-link").click(function(e){} 中添加此代码,因为您只需绑定一次。
最后一件事是你不需要绑定点击事件两次。 请参阅下面的示例,其中包含更新的代码 sn-p。我无法在浏览器中对此进行测试,因为您在 index (html) 中有代码,而不是单独的 JS 文件。

$(function ($) {
    $(document).ready(function () {
        $.ajaxSetup({cache: false});

        $(".post-link").click(function (e) {
            e.preventDefault();

            // this was moved from the function above, as you
            // don't need two events
            $(this).parent().addClass('fullscreen');
            $('.close').toggleClass('closeactive').removeClass('close');
            $(".back, .next").show();
            $('body').addClass('noscroll');

            var post_link = $(this).attr("href");

            $(this).siblings('#single-post-container').html("content loading");
            $(this).siblings('#single-post-container').load(post_link);
            window.history.pushState('object', 'New Title', post_link);
        });


        window.onpopstate = function (event) {
            // here you should add the logic to reverse (similar to what you have on the close event)
            $(this).parent().removeClass('fullscreen');
            $('.closeactive').toggleClass('close').removeClass('closeactive');
            $(".back, .next").hide();
            $('body').removeClass('noscroll');
        };
    });
});

第二点

这个问题有点复杂。
这里的一种方法是让您在 single.php 模板上添加与主页上相同的代码,并最初通过 javascript 显示该单曲。 或者在单个页面上添加一个重定向到主页,其中 GET 参数保存单个页面的值,例如http://www.andbloom.com/?s=funny-or-die
无论您在这里选择什么,您还需要更新您的 ajax 调用以创建一个单独的函数以仅返回您在 single.php 上拥有的(简单的 html)

其他说明

  • 您应该将代码从内联移至单独的文件并格式化。真的很难通读,而且我不能编辑东西,因为开发工具只允许编辑外部 JS 和 CSS 文件
  • 页面上有多个具有相同 ID 的元素。我指的是single-post-container
  • 对于与此功能相关的整个代码,您确实应该只有一个 $( document ).ready(function() {}

【讨论】:

  • 天哪,非常感谢!我确实按照您所说的做了,并将新代码实现为单独的文件。有几件事:后退按钮与前进按钮一起工作和不工作。因此,按下后退会隐藏关闭按钮,但不会隐藏下一个和上一个,或实际的帖子。我修改它以隐藏帖子,但感觉就像一个创可贴。这绝对不适用于前进按钮,我不确定如何指定浏览器后退按钮和浏览器前进按钮。其次,我不确定这部分该怎么做,“添加重定向......”“无论你在这里选择什么......”
猜你喜欢
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多