yii2 在视图中注册 js 代码 [英] yii2 register js code in a view

查看:24
本文介绍了yii2 在视图中注册 js 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 yii2 视图中注册 js 代码的最佳方式是什么?

What is the best way to register js code in yii2 view?

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

2

<?php 
     $this->registerJs('alert("hi");', View::POS_READY);
?>

3

<?php 
  $script = "function test() { alert('hi');}";    
  $this->registerJs($script, View::POS_END, 'my-options'); 
?>

推荐答案

Yii2,在视图中编写代码

Yii2, write code in views

<h2>Content</h2>
<?php
$script = <<< JS
    alert("Hi");
JS;
$this->registerJs($script);
?>

这篇关于yii2 在视图中注册 js 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆