php - jQuery:加载 PHP 代码片段

标签 php jquery load

我使用此代码将不同页面的内容加载到主页中:

$('#access a').click(function(event) {
    event.preventDefault();
    var url = $(this).attr('href');
$('#content').fadeOut(500, function() {
    $('#content').load(url, function() {
        $('#content').fadeIn(500);
        }); 
    });
});

我希望能够加载 <?php /*my PHP code */ ?> 的输出单击 #mybutton 时进入同一个容器。

我怎样才能做到这一点?

我怀疑它必须是非常基本的东西,但我不确定它是如何完成的。我将不胜感激专家的建议!

更新:这是我尝试运行的 php 代码:

<?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php if ( have_posts() ) : ?>

                <?php
                /* Run the loop for the search to output the results.
                 * If you want to overload this in a child theme then include a file
                 * called loop-search.php and that will be used instead.
                 */
                 get_template_part( 'loop', 'search' );
                ?>
<?php else : ?>
                <div id="post-0" class="post no-results not-found">
                    <h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
                    <div class="entry-content">
                        <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?></p>
                    </div><!-- .entry-content -->
                </div><!-- #post-0 -->
<?php endif; ?>

最佳答案

如果我理解正确的话,可能比你想象的更简单。

首先,用隐藏的 <div> 包装 PHP 输出:

<div id="searchResults" style="display: none;">
<?php printf( __( 'Search Results for: %s', 'twentyten' ), '<span>' . get_search_query() . '</span>' ); ?>
.....
.....
.....
<?php endif; ?>
</div>

然后只需单击按钮即可将这些内容复制到其他容器:

$('#mybutton').click(function(event) {
    event.preventDefault();
    $('#content').fadeOut(500, function() {
        $('#content').html($('#searchResults').html());
        $('#content').fadeIn(500);
    });
});

如果我不理解正确,请更好地解释,我会尝试找到其他方法来解决这个问题。

关于php - jQuery:加载 PHP 代码片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6679836/

相关文章:

java - 我如何清除整个数组?

php - 在特定字符后将结果保存为特定变量

php - 我可以使用一个执行两个操作的提交按钮吗?

jquery - asp.net mvc 3 razor 文件上传

jquery - PHP JQuery 复选框 - 如果预先选中复选框,则在未选中时不会删除 'checked' 属性

jquery - 为什么.load是绑定(bind)事件?

php - 编写托管在负载平衡服务器中的 Web 应用程序时需要了解哪些事项?

php - PHP 中的多层......正确的方法?

php - Twitter API 不响应整个推文

jquery - 跟踪用户输入的页面