html如何通过js脚本调用显示一个php页面的内容?

我要在一个html页面里显示下面这段php代码的输出内容。怎么将下面这段代码以 javascript 的语法输出

<?php
define('WP_USE_THEMES', false);
require('article/wp-blog-header.php'); //修改博客的路径
query_posts('showposts=20'); //显示文章条数
?>
<?php while (have_posts()): the_post(); ?>
<li><a href="<?php the_permalink(); ?>" target="_blank"><?php echo mb_strimwidth(strip_tags(apply_filters('the_title', $post->post_title)), 0, 50," "); ?></a></li>
<?php endwhile; ?>

用javascript形式输出数据,其实那就是php把js的输出脚本变为字符串的形式,当js调用会执行php代码

例如:输出的php文件demo.php

echo 'document.write("'.define('WP_USE_THEMES', false);.'")';
echo 'document.write("'.require('article/wp-blog-header.php');.'")';
echo 'document.write("'.query_posts('showposts=20');.'")';
其它的都是这样写就行了

html页面js调用demo.php
<script type="text/javascript" src="demo.php"></script>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-20
可以再文件里引用php文件<script type="text/javascript" src="demo.php"></script>,然后再demo.php里面用js代码写需要输出的内容就行了啊document.write(‘内容’);
第2个回答  2012-09-15
假如上述的php页面叫my.php.
新建的html中, body中含有.
<iframe id=xx/>
<script language=javascript>
document.getElementById("xx").src = "my.php";
</script>
第3个回答  2012-09-18
<?php
define('WP_USE_THEMES', false);
require('article/wp-blog-header.php'); //修改博客的路径
query_posts('showposts=20'); //显示文章条数
?>
<?php while (have_posts()): the_post(); ?>
document.write ('<li><a href="<?php the_permalink(); ?>" target="_blank"><?php echo mb_strimwidth(strip_tags(apply_filters('the_title', $post->post_title)), 0, 50," "); ?></a></li>');
<?php endwhile; ?>

然后你<script type="text/javascript" src="xxx.php"></script>就可以了