Yii2 如何更好的在页面注入JavaScript
2016-11-10 10:55
441 查看
先添加一个widgets
使用方法如下
<?php /** * User: yiqing * Date: 14-9-15 * Time: 下午12:09 */ namespace common\widgets; use yii\web\View ; use yii\widgets\Block ; class JsBlock extends Block{ /** * @var null */ public $key = null; /** * @var int */ public $pos = View::POS_END ; /** * Ends recording a block. * This method stops output buffering and saves the rendering result as a named block in the view. */ public function run() { $block = ob_get_clean(); if ($this->renderInPlace) { throw new \Exception("not implemented yet ! "); // echo $block; } $block = trim($block) ; /* $jsBlockPattern = '|^<script[^>]*>(.+?)</script>$|is'; if(preg_match($jsBlockPattern,$block)){ $block = preg_replace ( $jsBlockPattern , '${1}' , $block ); } */ $jsBlockPattern = '|^<script[^>]*>(?P<block_content>.+?)</script>$|is'; if(preg_match($jsBlockPattern,$block,$matches)){ $block = $matches['block_content']; } $this->view->registerJs($block, $this->pos,$this->key) ; } }
使用方法如下
<?php common\widgets\JsBlock::begin() ?> <script > $(function(){ jQuery(".company_introduce").slide({mainCell:".bd ul",effect:"left",autoPlay:true,mouseOverStop:true}); }); </script> <?php \year\widgets\JsBlock::end()?>
相关文章推荐
- Yii2 如何更好的在页面注入JavaScript
- Yii2 如何更好的在页面注入CSS
- 用户控件中使用了Javascript ,把2个控件放在页面上,那么页面上会有2个同名的javascript函数,如何处理
- 如何实现从服务器端向页面动态加载JavaScript脚本?
- 如何用javascript检测用户是否对页面的表单进行了修改?
- javascript 小技巧之如何在页面内加入日期
- javascript 小技巧之如何在页面内加入日期
- php如何更好更有效的实现-----用户注册页面
- 如何直接调试线上页面的JavaScript和CSS
- 如何实现服务器端下页面动态添加JavaScript脚本
- 如何在页面中添加焦点 JavaScript实例
- javascript如何在页面显示日期
- 如何在页面中引用JavaScript脚本?
- 如何实现服务器端下页面动态添加JavaScript脚本
- 如何实现服务器端下页面动态添加JavaScript脚本
- 如何在页面中添加焦点 JavaScript实例
- 小技巧:(javascript)如何让用户自由控制html页面大段文字的行间距和背景颜色
- 如何实现从服务器端向页面动态加载JavaScript脚本?
- (javascript)如何让用户自由控制html页面大段文字的行间距和背景颜色
- 如何在页面中引用JavaScript脚本?