在yii2视图中注册js代码的最佳方法是什么?
<?php
$this->registerJs(
'$("document").ready(function(){ alert("hi"); });'
);
?>
Run Code Online (Sandbox Code Playgroud)
<?php
$this->registerJs('alert("hi");', View::POS_READY);
?>
Run Code Online (Sandbox Code Playgroud)
<?php
$script = "function test() { alert('hi');}";
$this->registerJs($script, View::POS_END, 'my-options');
?>
Run Code Online (Sandbox Code Playgroud)
Iva*_*rus 22
Yii2,在视图中编写代码
<h2>Content</h2>
<?php
$script = <<< JS
alert("Hi");
JS;
$this->registerJs($script);
?>
Run Code Online (Sandbox Code Playgroud)
<?php
$this->registerJs( <<< EOT_JS_CODE
// JS code here
EOT_JS_CODE
);
?>
Run Code Online (Sandbox Code Playgroud)
所以你不必转义js代码
https://www.yiiframework.com/doc/guide/2.0/en/output-client-scripts