我是Yii框架(2.0版)的新手,我想在页面中添加一些jQuery代码:
$(document).ready(function(){
$('div').click(function(){
$(this).hide()
});
});
Run Code Online (Sandbox Code Playgroud)
当我点击它时,这段代码会隐藏div,我可以<script>在视图中声明吗?
我怎样才能在jQuery中编写这段代码?
MH2*_*2K9 12
首先建议你改($this)到$(this)和尝试这样的事情.
$script = <<< JS
$(document).ready(function(){
$('div').click(function(){
$(this).hide()
});
});
JS;
$this->registerJs($script, View::POS_END);
Run Code Online (Sandbox Code Playgroud)
你也可以使用View::POS_HEAD,View::POS_BEGIN与View::POS_READY替代View::POS_END.