在此先感谢您的帮助。我很难保持我的代码库干净。我想避免混合使用 PHP、HTML 和 CSS。
目前,我的主站点被分解成许多较小的选项卡。这些选项卡的 PHP 代码是在进行 ajax 调用后动态包含的。
elseif (file_exists('templates/custom/'.$center."/".$section."/".$tab.".php")) {
include 'templates/custom/'.$center."/".$section."/".$tab.".php";
}
效果很好,但我还想从外部文件中动态包含 JavaScript。在我看来,它会像这样工作,
elseif (file_exists('templates/custom/'.$center."/".$section."/".$tab.".php")) {
include 'templates/custom/'.$center."/".$section."/".$tab.".php";
include 'templates/custom/'.$center."/".$section."/".$tab.".js";
}
我如何根据用户想要转到的选项卡动态包含 javascript,同时仍然在各个文件中保持 javascript 由选项卡分隔。
我花了一整天的时间研究这个问题,并不断遇到类似这样的例子,
echo "<script language='javascript' type='text/javascript'>";
echo "alert('hello worldio');";
echo "</script>";
$URL="page.php";
echo "<script>location.href='$URL'</script>";
本网站是单页应用程序。再次感谢!
最佳答案
只是print
<script>
包含它的标签:
print '<script src="templates/custom/'.$center.'/'.$section.'/'.$tab.'.js'" type="text/javascript"></script>';
关于javascript - 从外部文件动态包含 JavaScript - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37219103/