Joomla怎样在页面加载css和js
//加载css文件
$document = &JFactory::getDocument();
$document -addStyleSheet(JURI::root()."css的路径");
举例:$document -addStyleSheet(JURI::root()."components/com_guestbook/css/guestbook.css");
//加载js文件
$document = &JFactory::getDocument();
$document -addScript(JURI::root()."js的路径");
$document = &JFactory::getDocument();
$document -addStyleSheet(JURI::root()."css的路径");
举例:$document -addStyleSheet(JURI::root()."components/com_guestbook/css/guestbook.css");
//加载js文件
$document = &JFactory::getDocument();
$document -addScript(JURI::root()."js的路径");
没有找到相关结果
已邀请:
3 个回复
QQ被盗了
赞同来自:
$doc-addStyleSheet("modules/mod_articletree/mod_articletree.css");
$app = JFactory::getApplication();
//if(!$app-jQuery)
{
//$doc-addStyleSheet(JURI::root()."/modules/mod_articletree/jQuery.js");
$doc-addStyleSheet("modules/mod_articletree/jQuery.js");
}
$doc-addStyleSheet("modules/mod_articletree/mod_articletree.js");
上面的代码不是用JURI::root()而是直接使用相对路径。当前的目录为网站的更目录,及index.php文件所在的位置
利用两种方式加载的css文件在浏览器中形式是不一样的。
如果使用$doc-addStyleSheet(JURI::root()."/modules/mod_articletree/jQuery.js");那么结果为:
而使用 $doc-addStyleSheet("modules/mod_articletree/jQuery.js");那么结果为:
上面代码有问题。加载js需要用addScript 函数,而不是用addStyleSheet。请读者自行更改
不是本人 - no no no!
赞同来自:
其实问题很简单,我使用了错误的方法加载js。应该使用addScript方法而我使用了addStyleSheet方法,最后js当然不执行了。
妥协 - 不会轻易妥协的人不愿意将就的人任性自由的人总是活得比较累
赞同来自:
可以使用下面的代码完成。
css代码: $doc = JFactory::getDocument();
$css = "#zmax_captcha_image img {
cursor: pointer;
}";
$doc-addStyleDeclaration($css);
加载js代码类似,使用$doc-addScriptDeclaration($js);