php - .load 不起作用,我想我的概念错了?

标签 php javascript jquery html

我已阅读 .load() 方法的文档和一些在线资源,但对于我正在构建的应用程序,它不起作用。请注意,我以前从未使用过 .load。

<script type="text/javascript">
$(document).ready(function() {
  $('#next').click(function(e){
    alert("Click"); // this alerts when clicked as expected
    $('#atestimonial').load('testimonial.php', function() {
        alert('Load was performed.');
    });
    return false;
  });
});
</script>

事件按预期进行。该代码块位于 header.php (使用 wordpress)中,而实际的 div #atestimonial 位于 sidebar.php 中,所以它位于不同的文件中;这有关系吗?

testimonial.php 的内部是这样的:

<?php 
include('testimonialPull.php');
echo "Bahh";
?>

testimonialPull.php

<?php
require_once('../../../wp-blog-header.php');
    query_posts(array(
        'cat' => 4,
        'order' => 'ASC', // ASC
        'orderby' => 'rand',
        'showposts' => 1,
    ));
    $wp_query->is_archive = true; $wp_query->is_home = false;
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif;
?>

如果我删除 testimonialPull 的内容,那么它会正确回显。

这只是将随机帖子放入证明 div 中。

实际的div和button的结构是这样的:

<div id="testimonials">

<div class="tHeading"><h4>Client Testimonials</h4></div>

<div class="atestimonial">
    <?php
    include('testimonial.php');
?>
</div>
<div id="next"><a id="nextC" href="#" return="false">NEXT TESTIMONIAL</a></div><!-- END #next -->

</div><!-- END #testimonials -->

那么,我是否做错了什么,导致推荐没有显示在带有 .load 的 div 中?

感谢您花时间阅读本文。

快速编辑

这正在本地主机上运行;我刚刚读到这不适用于本地主机,这是正确的吗?

最佳答案

您要求 jQuery 填充 $('#atestimonial'),这是一个 id 为 atestimonial 的元素,但您的文件中没有这样的元素页。

替换

<div class="atestimonial">

<div id="atestimonial">

编辑:

通过阅读您的评论,我认为您可能遇到路径问题,具体取决于您包含脚本的位置。

而不是

$('#atestimonial').load('testimonial.php', function() {

您可能需要类似的东西

$('#atestimonial').load('/testimonial.php', function() {

$('#atestimonial').load('/ggg/testimonial.php', function() {

关于php - .load 不起作用,我想我的概念错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12421828/

相关文章:

php - 我应该升级到 PHP 5.3 吗?

javascript - 基于 promise 的递归函数中的问题

javascript - 无法读取未定义的属性 'router'并且ko未在knockout JS中定义

javascript - 设置和清除间隔变成多个间隔

java - 如何使用 DES 在 Java 中加密数据并将该数据解密为 PHP

php - Zend_Controller_Router_Exception : "xyz" is not specified

php - Yii:如何在 View 中格式化日期时间并按年份搜索

Javascript 正则表达式在每第 3 个和第 4 个字符后添加破折号

javascript - 脚本仅适用于两个按钮之一

jquery - 出现错误:左侧赋值无效