使用 Yii 在 jQuery $(document).ready 中添加 javascript 的正确方法是什么
我正在使用 Yii 框架。在几个页面上,我想使用 jQuery 添加一些自定义 UI 内容。我希望结果是这样的:
$(document).ready(function() {
... my code
});
我找到了使用这样的建议:
Yii::app()->clientScript->registerScript('testscript',"
alert('hello world');
",CClientScript::POS_READY);
但是,我有一个相当大的 javascript 代码(大约 100 行),当它位于 PHP 字符串内部时甚至很难阅读它(编辑器中没有语法突出显示) ,没有代码折叠)。
一定有更好的方法吗?
我什至不介意将代码放在单独的 .js 文件中,但我不知道如何将其加载到 document.ready 处理程序中?
I'm using Yii framework. On a couple of pages I want to add some custom UI stuff using jQuery. I want the result to be something like:
$(document).ready(function() {
... my code
});
I found advice to use something like this:
Yii::app()->clientScript->registerScript('testscript',"
alert('hello world');
",CClientScript::POS_READY);
However, I have a rather large javascript code (about 100 lines) and it's hard to even read it when it's inside PHP string (no syntax highlighting in editor, no code folding).
There must be some better way to do it?
I wouldn't mind even placing the code in separate .js file, but I have no clue how to load it in to document.ready handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将代码隔离到 JS 文件中的类/函数中,使用 registerScriptFile 进行注册,然后使用 registerScript 调用该类/方法(然后将其添加到 CClientScript 使用的就绪方法中)。
Isolate your code into a class/function in a JS file, register that with registerScriptFile, then invoke that class / method with registerScript (will then be added to the ready-method which CClientScript uses.
好吧,我从来没有使用过 yii,但是在 Google 上搜索我发现你可以向 yii 添加 javascript。
我是 .net 人员,但此链接可能会对您有所帮助。
http ://www.yiiframework.com/forum/index.php?/topic/4778-what-is-the-best-form-to-load-jquery/
我只是猜测,但可能会看起来像这样...
在 myScript.js 中你会写...
现在我只是猜测,但你可能必须在这之前包含 jquery 插件。所以,它可能类似于......
(PHP)
(JQuery)
Well, I've never used yii, but searching around on Google I've found that you can add javascript to yii.
I'm a .net guy, but this link may help you out.
http://www.yiiframework.com/forum/index.php?/topic/4778-what-is-the-best-form-to-load-jquery/
I'm just guessing but it'll probably look like this...
And in myScript.js you'd write...
Now I'm just guessing, but you'd probably have to include the jquery plugin before any of this. So, its probably something like ...
(PHP)
(JQuery)