YII2 在試圖中寫js如何實現?
一、很多情況下我們需要在試圖中寫一個js代碼來調試一些功能,但是在YII2中你需要依賴YII2的資源,這樣的話怎麼寫呢。
首先我們直接把寫得js代碼賦值給一個PHP變量然後,再註冊一下這段js代碼。
<?php
$js = <<<JS
$("textarea").focus(function(){
.....
})
JS;
$this->registerJs($js);
?>
這樣就可以了,當然使用$this->registerJs($js);時要寫在
<?php $js = <<<JS $("textarea").focus(function(){ $(this).text('111'); }) JS; $this->registerJs($js); ?> <?php $this->endBody() ?> </body> </html> <?php $this->endPage() ?>前面。