我怎么能追加一行代码到一个HTML元素使用PHP
问题描述:
嗯,我知道我可以做到这一点与jquery,但我想不使用JavaScript,而是使用PHP。也从中学习。我怎么能追加一行代码到一个HTML元素使用PHP
我有一个文件夹层次这样
modules
|-navagation
|-js
|-nav.js
,我想找到一种方法来追加一条.js文件的
<head></head>
这样
<html>
<title></title>
<head>
<script scr="jquery.js"></script>
<script src="nav.js"></script>
</head>
</html>
我的目标是我想上传模块到我的模块文件夹,并让PHP自动提取并从m中加载资源odules。
我正在努力的一件事是我通过Smarty使用.tpl加载我的文件来显示我的标记。这就是我正在处理的唯一困境。
我原来的方法是这样的,但是因为我用模板文件来做它,所以我很茫然。
ob_start();
include(THEME_PATH . "/index.html");
$content = ob_get_contents();
ob_end_clean();
// Scripts to include
$scripts = BaseHelper::include_script("http://code.jquery.com/jquery-1.4.2.min.js");
$scripts .= BaseHelper::include_script("js/jquery.sound.js");
$scripts .= BaseHelper::include_script("js/jquery.jblock.js");
$scripts .= BaseHelper::include_script("js/init.js");
// Add scripts to theme
echo str_replace("</head>", $scripts . "\n</head>", $content);
答
我会变成你的index.html模板页到一个PHP页面,包括模板之前创建脚本之前变种,然后从包含的模板文件中使其:
ob_start();
// Scripts to include
$scripts = BaseHelper::include_script("http://code.jquery.com/jquery-1.4.2.min.js");
$scripts .= BaseHelper::include_script("js/jquery.sound.js");
$scripts .= BaseHelper::include_script("js/jquery.jblock.js");
$scripts .= BaseHelper::include_script("js/init.js");
include(THEME_PATH . "/index.php");
$content = ob_get_contents();
ob_end_clean();
指数.php:
<head>
<?php echo $scripts ?>
</head>
我不完全理解你的问题。但尝试'' – Khez 2011-04-16 08:11:03
*(related)* [最佳解析方法](http://stackoverflow.com/questions/3577641/best-methods -to-parse-html/3577662#3577662) – Gordon 2011-04-16 08:11:09
如果你想修改DOM并且已经知道jQuery,那么你可以使用phpQuery(见上面的链接)。但是,根据您的模板,将脚本内容传递到模板而不是更改DOM可能更容易。基本上,当你要用DOM修改模板时,几乎不需要使用Smarty,因为DOM可以完全替代Smarty。 – Gordon 2011-04-16 08:12:27