When I first come into contact with Yii, I may be a little confused about the resource management of assets inside. For example, when using jquery, some of the widgets that come with Yii's system will automatically load jquery. But what if you don't have these widgets and want to load jquery?
The solution provided by yii is: Yii::app()->clientScript->registerCoreScript('jquery'); . Through this registration statement, jquery is prevented from being called twice. If the system (recommended learning: yii tutorial)
is automatically loaded, it will not be registered. If not, Then load jquery through this statement.
Code:
<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.js" type="text/javascript"></script> <script src="<?php echo Yii::app()->request->baseUrl; ?>/js/nav.js" type="text/javascript"></script> <script src="<?php echo Yii::app()->request->baseUrl; ?>/js/menu.js" type="text/javascript"></script>
php Code:
<?phpYii::app()->clientScript->registerCoreScript('jquery'); Yii::app()->clientScript->registerScriptFile(Yii::app()->request- >baseUrl.'/js/nav.js'); Yii::app()->clientScript->registerScriptFile(Yii::app()->request- >baseUrl.'/js/menu.js');?>
The above is the detailed content of How to use jquery in yii. For more information, please follow other related articles on the PHP Chinese website!