Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP代码放入JavaScript document.write中_Javascript_Php_Wordpress_Document.write - Fatal编程技术网

如何将PHP代码放入JavaScript document.write中

如何将PHP代码放入JavaScript document.write中,javascript,php,wordpress,document.write,Javascript,Php,Wordpress,Document.write,您好,有人能帮我修复这个代码吗 第一代码 <script type="text/javascript"> if (document.getElementById("tester") != undefined) { document.write('**2nd code should be here**'); } else { document.write('<img scr="./img.png" />'); } </script> if(d

您好,有人能帮我修复这个代码吗

第一代码

<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
    document.write('**2nd code should be here**');
}
else
{
    document.write('<img scr="./img.png" />');
}
</script>

if(document.getElementById(“tester”)!=未定义)
{
编写(“**第二个代码应在此处**”);
}
其他的
{
文件。写(“”);
}
第二代码

<?php while (have_posts()) : the_post(); get_template_part('item-video');
endwhile; ?>


我要做的是在第一个代码中插入第二个代码。但是当我尝试插入它时,它会给我一个空白页。

我听到的最大的事情是PHP是服务器端,而Javascript是客户端。因此,除非重新加载页面,否则使用document.write()更改html页面不会有任何作用。我认为您可以通过使用Ajax和load()函数绕过这个问题

为什么不直接将其打印到正文中,而不使用
文档。编写
?@DontVoteMeDown我已经尝试过了,但它给了我一个空白页。你不应该将PHP与JavaScript混合使用。您不能在PHP中也执行
tester
-条件吗?然后,您的
the_post()
函数有问题。它用
print
echo
?也给我们看看它的内容。@Bergi我不知道如何设置条件。不管怎样,我正在试这个