yii2 php怎么引入css,yii框架怎么引入css与js文件

b324fcb1a56899433666308caf730ef6.png

1、可以直接在视图页面上引入

eea6f2189b2f04f1fb22d5ceb471ed50.png

2、可以直接写原生代码引入,路径是项目目录/web/css 或者/js

相关教程推荐:yii框架

3、可以使用assetBundle管理css样式及js脚本

资源包定义:basic/assets/AppAsset.php<?php /**

* @link http://www.yiiframework.com/

* @copyright Copyright (c) 2008 Yii Software LLC

* @license http://www.yiiframework.com/license/

*/

namespace app\assets;

use yii\web\AssetBundle;

/**

* @author Qiang Xue

* @since 2.0

*/class AppAsset extends AssetBundle{

public $basePath = '@webroot';

public $baseUrl = '@web';

public $css = [

'css/site.css',

'css/base.css'

];

public $js = [

'js/sliders.js'

];

public $depends = [ //依赖包,没有可以不写

'yii\web\YiiAsset',

'yii\bootstrap\BootstrapAsset',

];

//定义按需加载JS方法,注意加载顺序在最后

public static function addScript($view, $jsfile) {

$view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);

}

//定义按需加载css方法,注意加载顺序在最后

public static function addCss($view, $cssfile) {

$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);

} }

在视图文件开头写入:<?php

use yii\helpers\Html;

use app\assets\AppAsset;

AppAsset::register($this);

?>

到现在为止,我们可以在浏览器上测试,发现并没有引入css和js文件,这里要注意了,我们还需要最后一步:

在视图文件中我们要加入一下代码(注:如果我们使用公共视图文件,可以加入到公共视图文件,如果没有使用,可以放到单独页面中)

f79510fcd20e3f35cf0622f0dd6de693.png

4、不需要在资源包管理器中定义方法,只要在视图页面中直接引入即可AppAsset::register($this);

//css定义一样

$this->registerCssFile('@web/css/font-awesome.min.css',['depends'=>['api\assets\AppAsset']]);

$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset']]);

//$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['api\assets\AppAsset'],'position'=>$this::POS_HEAD]);

更多编程相关内容学习,请访问php中文网编程教程栏目!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值