zoukankan      html  css  js  c++  java
  • BUG YII2.0 $ is not defined

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

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

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

    解决方案:

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

    public $jsOptions = array(
        'position' => yiiwebView::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',
        ['ackendassetsAppAsset'], 
        ['position' => 'yiiwebView::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.


  • 相关阅读:
    codevs 1432 总数统计
    codevs3500 快速幂入门题解
    #163. 【清华集训2015】新式计算机
    2989:糖果
    191:钉子和小球
    java 删除所有HTML工具类
    DateTools时间插件
    新的开始
    springBoot---端口,路径数据配置
    springBoot---多个配置文件读取
  • 原文地址:https://www.cnblogs.com/pcx105/p/7807719.html
Copyright © 2011-2022 走看看