我使用此代码将不同页面的内容加载到主页中:
$('#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/