如何在 Yii 2.0 中将 jQuery 添加到我的页面?
在 Yii 1.x 中你可以使用:
Yii::app()->clientScript->registerCoreScript('jquery');
我已经尝试用自己的 View 类重写并尝试在那里注册 jQuery,但它没有显示在我的 html 页面中:
namespace frontend\components;
/**
* This is the base view object, it extends the yii\web\View so you can add custom view stuff here.
*/
class BaseView extends \yii\web\View {
public function init()
{
parent::init();
\yii\web\JqueryAsset::register($this);
}
}
我想你使用了正确的注册 jquery 的方法。而且,如果您注册至少一个位置为 POS_READY 的 js,Yii 2.0 基本视图类也会自动注册 jquery,例如(在视图内):
$this->registerJs($js, \yii\base\View::POS_READY);
或在任何控制器或小部件内:
$this->getView()->registerJs($js, \yii\base\View::POS_READY);
因此,如果您使用任何 javascript 代码或文件(您使用它,因为您需要 jquery),您可以完全摆脱该 Jquery 资源。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)