在 Joomla 中添加页面特定的 javascript 或 CSS [英] Add page-specific javascript or CSS in Joomla
问题描述
如何只在 Joomla 中的某篇文章中包含 javascript 或 CSS 文件?
How do you include a javascript or CSS file only on a certain article in Joomla?
我有一篇文章需要 jQuery UI 和相关主题.因为它没有在任何其他页面上使用,所以我只希望在这篇特定的文章中使用它.在文章的 HTML 中添加必要的 和
标记不起作用,因为它们被剥离了保存时.
I have one article which requires the jQuery UI and associated theme. Since this isn't being used on any other pages, I only want it for this particular article. Adding in the necessary <script>
and <link rel="stylesheet">
tags in the HTML of the article doesn't work, since they get stripped out when saved.
如果有一种方法可以包含某些文件,或者停止剥离这些标签,那就太好了.
If there was a method to include certain files, or to stop the stripping of those tags, that'd be really good.
推荐答案
我最终创建了一个插件来为我维护的站点执行此操作;我可能会发布它的代码,但解释起来很简单.您构建了一个内容插件,用于搜索诸如 {css path/to/a/css/file.css}
之类的标签,将它们去除,然后将这些文件添加到 HTML ;代码>.获得要包含的 CSS 或 JS 文件的名称后,以下是用于在标题中获取它的代码:
I ended up creating a plugin to do this for a site I maintain; I may release the code for it, but it's pretty simple to explain. You build a content plugin that searches for tags like {css path/to/a/css/file.css}
, strips them out, then adds the files to the HTML <head>
. Once you have the name of the CSS or JS file you want to include, here is the code you use to get it in the header:
$document =& JFactory::getDocument();
$document->addStyleSheet(JURI::base() . 'path/to/style.css');
$document->addScript(JURI::base() . 'path/to/script.js');
这篇关于在 Joomla 中添加页面特定的 javascript 或 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!