Yii::app()->getClientScript()->registerCssFile(Yii::app()->theme->baseUrl.'/css/jquery.ad-gallery.css');
Yii::app()->getClientScript()->registerScriptFile(Yii::app()->theme->baseUrl.'/js/jquery.ad-gallery.js', CClientScript::POS_HEAD);
我只想为一个特定 View 加载这些文件
或者在 Yii 框架中为特定 View 导入 CSS 和 Javascript 的任何更好的想法
最佳答案
像这样:
public function actionIndex()
{
$uri = 'path to your action-specific css';
Yii::app()->clientScript->registerCssFile($uri, 'screen, projection');
}
像这样,如果你想要特定于 Controller 的文件:
public function init()
{
$uri = 'path to your controller-specific css';
Yii::app()->clientScript->registerCssFile($uri, 'screen, projection');
return parent::init();
}
如果您想要在所有 Controller 上使用此功能,请创建 CController 的子类,将这些方法放在那里,然后更改您的 Controller 以使用新的子类。
这绝对对你有帮助
关于javascript - 在yii controller中注册css和js是不是更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637444/