来源:https://www.cnblogs.com/attitudeY/p/6279985.html

BUG描述:$ is not defined 没有加载jquery成功

原因:Yii2.0将JS代码默认加载页面加载后

解决方案:

第一种方案:最简单方法是在 assets\AppAsset.php 中加上,页面前加载

public $jsOptions = array(
'position' => \yii\web\View::POS_HEAD
);

第二种方案:But in production you usually want the scripts to load last, and instead you let Yii2 handle your javascript:

$this->registerJs(
'$("document").ready(function(){ alert("hi"); });'
);

Now Yii will handle this js and place it after anything important (like jQuery).

You'll however soon notice that IDE's are usually bad at handling this kind of language nesting (JavaScript inside PHP) so the syntax highlighting is likely to break. One way to solve it is to register your script in a separate file:

$this->registerJsFile( 'myScript.js' );

If you want even more control about which order to load your scripts, you can add dependencies as your second argument, and additional options as the third:

$this->registerJsFile(
'myScript.js',
['\backend\assets\AppAsset'],
['position' => '\yii\web\View::POS_END']
);

If you for some reason absolutely want the script to be rendered inline you can do:

$this->registerJs( $this->renderPartial('myScript.js') );

The recommended way to add your scripts is to use AssetBundles. Look in assets/AppAssets.php and add your js-file to the $js-array.


BUG YII2.0 $ is not defined的更多相关文章

  1. Yii2.0

    Yii2.0基础框架   缘起 因为一个月的短暂停留,我在给朋友搞事情,所以Yii系列的文章耽搁了很长时间,现在又重拾当时的知识,给大伙好好撸下这一系列的博客 提起Yii,虽然是国外的开发者搞的,但是 ...

  2. 【转】PHP框架性能测试报告 - ThinkPHP 3.2.3 Laravel 5.2 Yii2.0.5

    作为一个PHP开发者,而且是初创企业团队的技术开发者,选择开发框架是个很艰难的事情. 用ThinkPHP的话,招聘一个刚从培训机构出来的开发者就可以上手了,但是性能和后期代码解耦是个让人头疼的事情.不 ...

  3. 在beforeAction里redirect无效,Yii2.0.8

    我是在官方GitHub上得到回答,试了一下,确实解决问题了.之前的问题描述: 之前是2.0.3,然后用composer直接升级到2.0.8,就不正常了,以为是我代码的问题,于是再次尝试 用compos ...

  4. 关于在Windows下Composer下载安装Yii2.0

    先是composer的安装,主要有两个方式,一个直接下载安装包安装,Composer-steup.exe文件,第二种直接下载composer.phar文件,用php去运行这个文件可以一样起到作用,之后 ...

  5. Yii1.1应用升级到Yii2.0的一些注意点

    1. 应用程序实例直接使用全局命名变量来訪问:$app,而无须调用app() 2. Yii2在视图层的一个显著改变是引入了视图类,这样在MVC模式实现上更为完整. 对应的,相关展现层附属类就由新的视图 ...

  6. BUG YII2.0 cURL error 6: Could not resolve host:

    BUG描述:登录直接显示 原因:服务器设置端口权限,或者DNS毛病 解决方案:只能去服务器端设置,配置端口 DNS: 修改dns 114.114.114.114 或者 8.8.8.8

  7. 学习yii2.0——行为

    学习yii框架的行为之前,需要先了解yii的事件,可以参考这篇博客: 怎么理解行为 yii框架的行为有点类似于trait,可以有个大体的概念: 1.有一个类A,包含一些属性和方法,很普通的一个类A. ...

  8. MacBook Pro 安装composer及Yii2.0

    最近想看看Yii的一些东西,需要安装一下composer curl -sS https://getcomposer.org/installer | php 此操作会下载最新版本到当前的目录下 然后将下 ...

  9. yii2——自定义widget

    参考资料:http://www.bsourcecode.com/yiiframework2/how-to-create-custom-widget-in-yii2-0-framework/   如何使 ...

随机推荐

  1. TOJ1302: 简单计算器 && TOJ 4873: 表达式求值&&TOJ3231: 表达式求值

    这些都是应用Python的eval函数的一些题目! TOJ1302传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=show ...

  2. TOJ 4383 n % ( pow( p , 2) ) ===0

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4383 描述 There is a ...

  3. 线程 Thread Handler

    new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = 0; ...

  4. 一个先进的App框架:使用Ionic创建一个简单的APP

    原文  http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...

  5. Number & Maths类

    Java Number & Math 类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 实例 int a = 5000;float ...

  6. 解决Eclipse添加新server时无法选择Tomcat7的问题

    在Eclipse中创建了一个Web工程后,需要将该工程部署到Tomcat中进行发布.有时就会遇到在New Server对话框中选择了Tomcat 6/7后却无法单击“Next”按钮的问题,如下图所示: ...

  7. 在BCH硬分叉后防止重放攻击-2

    重放攻击原理和防范措施——如何安全分离BCH的分来源:巴比特018-11-18 16:49:37 热度 12390 第0章 引言 即将面临的比特币分裂,如何保证你的币在分裂后肯定留下两种币?一个重点要 ...

  8. Task.WaitAll代替WaitHandle.WaitAll

    Task.Waitall阻塞了当前线程直到全完.whenall开启个新监控线程去判读括号里的所有线程执行情况并立即返回,等都完成了就退出监控线程并返回监控数据. task.Result会等待异步方法返 ...

  9. spring boot 1.5.3项目放到resin4.0.53报错

    报错信息: {main} java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImp ...

  10. Android Studio 使用入门

    Android Studio 快捷键 Action Mac OSX Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ct ...