【问题标题】:ajax refresh the whole page inside a div instead of reloading the div contentajax 刷新 div 内的整个页面,而不是重新加载 div 内容
【发布时间】:2015-05-12 23:18:12
【问题描述】:

Ajax 刷新 div。但问题是它刷新了 div 内的整个页面。它就像页内页。我知道我做错了什么。

我正在开发一个 wordpress 网站(本地)。我想在节目开始后立即更改演示者的图像。 Ajax 脚本看起来不错,但不能按我想要的方式工作。所以,我需要你们的帮助............

我把我的代码放在下面。我要刷新的 div 在布局页面中,而 ajax 脚本在页脚页面中

Wordpress 模板:

<!DOCTYPE html>

<!--[if IE 8]> <html class="ie ie8" <?php language_attributes(); ?>> <![endif]-->
<!--[if IE 9]> <html class="ie ie9" <?php language_attributes(); ?>> <![endif]-->
<!--[if gt IE 9]><!--> <html <?php language_attributes(); ?>> <!--<![endif]-->

<head>

<?php
/**
 * Match wp_head() indent level
 */
?>

<meta charset="<?php bloginfo('charset'); ?>" />
<title><?php wp_title(''); // stay compatible with SEO plugins ?></title>

<?php if (!Bunyad::options()->no_responsive): // don't add if responsiveness disabled ?>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<?php endif; ?>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />

<?php if (Bunyad::options()->favicon): ?>
<link rel="shortcut icon" href="<?php echo esc_attr(Bunyad::options()->favicon); ?>" />
<?php endif; ?>

<?php if (Bunyad::options()->apple_icon): ?>
<link rel="apple-touch-icon-precomposed" href="<?php echo esc_attr(Bunyad::options()->apple_icon); ?>" />
<?php endif; ?>

<?php wp_head(); ?>

<!--[if lt IE 9]>
<script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script>
<![endif]-->

</head>

<body <?php body_class(); ?>>

  <div class="main-wrap">

    <?php

    /**
     * Get the partial template for top bar
     */
    get_template_part('partials/header/top-bar');

    ?>

    <div id="main-head" class="main-head">

        <div class="wrap">


            <?php

                /**
                 * Get the header based on settings
                 */
                $header = Bunyad::options()->header_style ? Bunyad::options()->header_style : 'default';

                get_template_part('partials/header/' . $header);


                /**
                 * Setup data variables to enable or disable sticky nav functionality
                 */
                $attribs = array('class' => array('navigation cf', Bunyad::options()->nav_align));

                if (Bunyad::options()->sticky_nav) {

                    $attribs['data-sticky-nav'] = 1;

                    // sticky navigation logo?
                    if (Bunyad::options()->sticky_nav_logo) {
                        $attribs['data-sticky-logo'] = 1;
                    }
                }

            ?>

            <nav <?php Bunyad::markup()->attribs('navigation', $attribs); ?>>

                <div class="mobile" data-type="<?php echo Bunyad::options()->mobile_menu_type; ?>" data-search="<?php echo Bunyad::options()->mobile_nav_search; ?>">
                    <a href="#" class="selected">
                        <span class="text"><?php _e('Navigate', 'bunyad'); ?></span><span class="current"></span> <i class="hamburger fa fa-bars"></i>
                    </a>
                </div>

                <?php wp_nav_menu(array('theme_location' => 'main', 'fallback_cb' => '', 'walker' =>  'Bunyad_Menu_Walker')); ?>
            </nav>

        </div>

    </div>

  <?php if (!Bunyad::options()->disable_breadcrumbs): ?>
    <div class="wrap">
        <?php Bunyad::core()->breadcrumbs(); ?>
    </div>
  <?php endif; ?>

  <?php do_action('bunyad_pre_main_content'); ?>

**Layout page:**

<?php
/*
    Template Name: Radio Player
*/

get_header('radio');


if (Bunyad::posts()->meta('featured_slider')):
    get_template_part('partial-sliders');
endif;

?>


  <div class="main wrap cf">

  <div class="row">
    <div class="col-8 main-content">

        <?php if (have_posts()): the_post(); endif; // load the page ?>

        <div id="post-<?php the_ID(); ?>" <?php post_class('page-content'); ?>>

        <?php if (Bunyad::posts()->meta('page_title') != 'no'): ?>

            <header class="post-header">

            <?php if (has_post_thumbnail()): ?>
                <div class="featured">
                    <a href="<?php $url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full'); echo $url[0]; ?>" title="<?php the_title_attribute(); ?>">

                    <?php if ((!in_the_loop() && Bunyad::posts()->meta('layout_style') == 'full') OR Bunyad::core()->get_sidebar() == 'none'): // largest images - no sidebar? ?>

                        <?php the_post_thumbnail('main-full', array('title' => strip_tags(get_the_title()))); ?>

                    <?php else: ?>

                        <?php the_post_thumbnail('main-slider', array('title' => strip_tags(get_the_title()))); ?>

                    <?php endif; ?>

                    </a>
                </div>
            <?php endif; ?>

            </header><!-- .post-header -->

        <?php endif; ?>

            <div class="refreshMe">
        <?php Bunyad::posts()->the_content(); ?>


        </div>

    </div>

  </div> <!-- .row -->
  </div> <!-- .main -->
</div>


<?php get_footer('radio'); ?>


**i put my refresh div on the layout page with class name "refreshMe

Footer.php**


    <?php do_action('bunyad_post_main_content'); ?>

    <footer class="main-footer">

     <p>
         Copyright &copy; Babylon Radio 2015
    </p>

    </footer>

</div> <!-- .main-wrap -->

<?php wp_enqueue_script("jquery"); ?>
<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.timers-1.0.0.js"></script>
<script type="text/javascript">

jQuery(document).ready(function(){
    var j = jQuery.noConflict();
    j(document).ready(function()
    {
        j(".refreshMe").everyTime(10000,function(i){
            j.ajax({
                cache: false,
                success: function(html){
                j(".refreshMe").html(html);
                }
            })
        })
    });
    j('.refreshMe').css({color:"red"});
});
</script>

<?php wp_footer(); ?>

</body>
</html>

**and my ajax script is in the footer page.**

Help me out with this

【问题讨论】:

  • google wordpress jQuery ajax 教程,您的 ajax 函数有很多问题(缺少 url、缺少操作、未向服务器传递任何内容)。您也没有显示处理 ajax 提交的 php 函数。所以目前还没有多少人可以帮助你。

标签: javascript php ajax wordpress


【解决方案1】:

我不熟悉 wordpress,但在 ajax 方面,您必须有一个 url,请求将通过该 url 为您的 div 获取新内容。例如,您的页面位于 example1.com,因此您必须为 example1.com/get-my-div 创建一个菜单处理程序, 因此该页面仅返回您的 div 内容。 ajax 调用中的 url 被省略,默认为当前页面。这就是为什么你在页面里面有一个页面,因为整个页面的内容都显示在你的 div 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多