【问题标题】:Can i use jQuery .load while still changing URL?我可以在更改 URL 的同时使用 jQuery .load 吗?
【发布时间】:2016-03-08 20:35:28
【问题描述】:

我想要达到的目标

我希望能够无缝切换this 网站侧边栏中的不同帖子,同时为我的帖子使用自定义帖子模板。我所说的无缝是指每次导航时屏幕都不会变白。

我的问题

single.php 或 single-{slug}.php 不起作用,因为在导航侧边栏时 URL 不会改变

我正在使用的插件

自定义帖子类型用户界面

Ajax 内容渲染器

我的侧边栏的 jQuery 脚本

$(document).ready(function() {
    $("#sidebar li:has(a)").click(function() {

        var page = $("a:first",this).attr("href");

        $("#content").load(page);
        return false;
    });
});

用于侧边栏和内容区域的 HTML 和 PHP

<div class="row"> <!-- Main content row -->
    <div class="col-md-4" id="sidebar">
        <?php
        if (is_page('gulvservice')){
            wp_nav_menu(array('menu'=>'gulvservice' ));
        } elseif (is_page('malerservice')) {
            wp_nav_menu(array('menu'=>'malerservice' ));
        } elseif (is_page('industrilakering')) {
            wp_nav_menu(array('menu'=>'industrilakering' ));
        }
        ?>
    </div>

    <div class="col-md-8" id="content">
        <?php  if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <div class="title"><h1>
                    <?php the_title(); ?>
                </h1></div>
            <div class="div">
                <?php
                if( has_post_thumbnail() ) {
                    the_post_thumbnail();
                }
                ?>
            </div>
        <?php endwhile; endif; ?>
    </div>
</div>
</div> <!-- Main Content -->

【问题讨论】:

    标签: php jquery templates wordpress


    【解决方案1】:

    需要阻止&lt;a&gt; 上的事件。所以最好把选择器移到那些元素上

    试试:

    $(document).ready(function() {
      $("#sidebar li a").click(function() {
        var page = $(this).attr("href");
        $("#content").load(page);
        return false;
      });
    });
    

    【讨论】:

    • 我不敢相信它这么容易。然而,它引起了另一个问题(显然),li 本身不再是可点击的,这是预期的。你知道解决这个问题的方法吗?
    • 尝试添加event 参数而不是return false 使用event.preventDefault()
    • 谢谢,我会试试的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2021-12-04
    • 2012-02-21
    相关资源
    最近更新 更多