来源: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. TOJ 3151: H1N1's Problem(欧拉降幂)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...

  2. 187. Repeated DNA Sequences (String; Bit)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  3. 8.17 纯css画一个着重号图标

    今天看到一个同事写的着重号图标,我以为是图片,仔细一看,是span标签!哇!!学习一下哈哈 图标长这样: CSS代码: .hint{ display: inline-block; width: 20p ...

  4. Exceptions

    [定义] error: external, like out of memory exception: internal, like file not found 父类都是throwable 逻辑有错 ...

  5. 字符串加u的特殊需求

    #coding:utf-8 L = ['a','b','c'] S = [] for i in L: tmp = str(i).decode('utf-8') S.append(tmp) print ...

  6. Git两分钟指南-学习入门参考

    Git两分钟指南 http://blog.jobbole.com/78999/ GIT和SVN之间的五个基本区别 http://www.oschina.net/news/12542/git-and-s ...

  7. Windows服务安装、卸载、启动和关闭的管理器

    最近在重构公司的系统,把一些需要独立执行.并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了.但是每次都要建立Windows服务项目 ...

  8. angular2.0学习笔记1.开发环境搭建 (node.js和npm的安装)

    开发环境, 1.安装Node.js®和npm, node 6.9.x 和 npm 3.x.x 以上的版本. 更老的版本可能会出现错误,更新的版本则没问题. 控制台窗口中运行命令 node -v 和 n ...

  9. Luogu 3421 [POI2005]SKO-Knights - Exgcd

    Description 给出一个骑士的 $N$种 中行走的方式 $(a_i, b_i)$, 可以使骑士的坐标$(-a,-b)$或$(+a,+b)$. 我们需要找出 第二个骑士的 两种行走方式 $(c_ ...

  10. echo 变量不加引号出错

    result=`ps aux  | grep “×××” |grep -v “×××” start_time=$(echo $result | awk '{print $9}') 问题:发现输出是 s ...