hebo5485 2016-06-15 16:34:16 4610次浏览 4条回复 0 0 0

在yii2 advanced版的frontend中的视图引入CDN上的js文件,然而却报错了 代码如下

<?php

use yii\helpers\Html;
use yii\helpers\Url;

$this->title = '表格';
$this->params['breadcrumbs'][] = ['label' => 'Tool', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

?>
<html>
<head>
    <?php $this->registerJsFile("http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js") ?>
    <?php $this->registerJsFile("http://cdn.hcharts.cn/highcharts/highcharts.js") ?>
    <script>
        $(function () {
            $('#container').highcharts({
                chart: {
                    type: 'area'
                },
                title: {
                    text: 'skyline traveler '
                },
                xAxis: {
                    categories: ['a', 'b', 'c']
                },
                yAxis: {
                    title: {
                        text: 'sss'
                    }
                },
                series: [{
                    name: 'hhh',
                    data: [1, 0, 4, 0]
                }, {
                    name: 'kkk',
                    data: [2, 59, 3, 0]
                }, {
                    name: 'kkk',
                    data: [3, 71, 3, 0]
                }, {
                    name: 'kkk',
                    data: [4, 12, 3, 0]
                }, {
                    name: 'kkk',
                    data: [5, 27, 3, 0]
                }, {
                    name: 'kkk',
                    data: [25, 17, 3, 0]
                }, {
                    name: 'kkk',
                    data: [35, 27, 3, 0]
                }, {
                    name: 'kkk',
                    data: [75, 7, 3, 0]
                }]
            });
        });

    </script>
</head>
<body>

<div id="container" style="min-width: 720px;height: 540px"></div>
</body>
</html>

用的是highcharts的,在单独的php文件中写入就没有问题,然而在框架中却不显示并且报错 "charts:46 Uncaught ReferenceError: $ is not defined" 请大神指教!

  • 回复于 2016-06-16 09:21 举报

    $this->registerJsFile('xxx.js', View::POS_END)

  • 回复于 2016-06-16 14:27 举报

    默认registerJsFile位置是在END,js调用时库还没引入。
    JS代码用registerJs调用 或把 registerJsFile的位置往前调整。

  • 回复于 2016-06-16 16:37 举报

    谢谢两位解答,然而还是报错:

    PHP Warning – yii\base\ErrorException
    
    Cannot use a scalar value as an array
    1. in E:\www\yii2test\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 261
     *
         * The rest of the options will be rendered as the attributes of the resulting script tag. The values will
         * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
         * See [[renderTagAttributes()]] for details on how attributes are being rendered.
         * @return string the generated script tag
         * @see Url::to()
         */
        public static function jsFile($url, $options = [])
        {
            $options['src'] = Url::to($url);
            if (isset($options['condition'])) {
                $condition = $options['condition'];
                unset($options['condition']);
                return self::wrapIntoCondition(static::tag('script', '', $options), $condition);
            } else {
                return static::tag('script', '', $options);
            }
        }
    
  • 回复于 2016-06-17 12:43 举报
    $this->registerJsFile('path/to/js', [
        "depends" => [
            //jquery asset or other asset
        ]
    ]);
    
您需要登录后才可以回复。登录 | 立即注册