Yii中引入js和css文件

158 阅读1分钟
Yii中引入js和css文件在模板下直接引入

在模板下直接引入:
1
2
3
4
5
6
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl .
"/js/common.js");
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl .
"/js/blog.js");
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl .
"/css/style.css");

也可加个参数说明在什么位置加入:

1
2
3
4
5
6
7
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl .
"/js/common.js", CClientScript::POS_END);
CClientScript::POS_HEAD : the script is insertedinthe head section right before the title element.
CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
CClientScript::POS_END : the script is inserted at the end of the body section.
CClientScript::POS_LOAD : the script is insertedinthe window.onload()function.
CClientScript::POS_READY : the script is insertedinthe jQuery's readyfunction.

引入jquery:

1Yii::app()->clientScript->registerCoreScript('jquery');

创建JS代码:

1
2
3
4
5
6
$cs = Yii::app()->clientScript->registerCoreScript('jquery');
$cs->registerScript('abc',
"$('#a').click(function(){
alert('aa');
})"
);