日期:2014-05-17  浏览次数:20492 次

Yii框架中使用jquery的问题!
Yii框架下的protected/views/site/index.php,一个非常简单的页面
PHP code

<?php 
Yii::app()->clientScript->registerCoreScript(Yii::app()->request->baseUrl.'/protected/views/site/jquery.js');
Yii::app()->clientScript->registerCoreScript(Yii::app()->request->baseUrl.'/protected/views/site/1.js');
?>

 <p>123</p>
 <button>hide</button>


可是jquery效果完全显示不了,我将文件移出Yii框架的文件夹,独立运行,并把代码改成如下
PHP code

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="1.js"></script>
<p>123</p>
 <button>hide</button>


效果可以显示,证明js文件没问题,请问在Yii中怎样有效使用jquery呢?


------解决方案--------------------
具体看
http://hi.baidu.com/wastorode/blog/item/23bbccf86e0a7d93b901a0b5.html
------解决方案--------------------
有两个地方我个人感觉有问题:
1.registerCoreScript()----手册上是这样写的:
public void registerCoreScript(string $name) 
$name --- the core javascript library name 
就是说这方法应该是用来加载专门的JS库,如jquery;
你要加载你的1.js,应该直接用Yii::app()->clientScript->registerScriptFile()这个方法

2.js文件最好在protect外独立开个js的文件夹放js代码,感觉上会好些
(以上纯属个人看法,不对之处,多多包涵)