Javascript 如何在 Yii 框架中使用 jQuery。?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12796021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use jQuery with the Yii framework.?
提问by raghul
How to use jquery/javascript in yii?,
如何在 yii 中使用 jquery/javascript?,
How to use my Script in yii?
如何在 yii 中使用我的脚本?
Why is this any different from using jQuery in any other way?
为什么这与以任何其他方式使用 jQuery 有什么不同?
回答by raghul
using Yii's registerScript to load our jQuery, which if used normally lookslike this:
使用 Yii 的 registerScript 加载我们的 jQuery,如果正常使用,它看起来像这样:
$(document).ready(function(){
alert('hello');
// Enter code here
});
Now if we use Yii's Scriptlike this:
现在,如果我们像这样使用Yii 的脚本:
<?php
Yii::app()->clientScript->registerScript('helloscript',"
alert('hello');
",CClientScript::POS_READY);
?>
You will see that you need to pass 3 things to the Yii::app()->clientScript->registerScript function, firstly a script name, which is not really important; it must just bee unique from any other script name on the same page. LINK
你会看到你需要向 Yii::app()->clientScript->registerScript 函数传递 3 个东西,首先是一个脚本名称,这并不重要;它必须与同一页面上的任何其他脚本名称唯一。关联
回答by Sammaye
How to use jquery in yii?
如何在 yii 中使用 jquery?
As you stated above you can either register a new script block or you can register a new external script file. You cna also register assets within plugins which will take entire JS folders and combine them into your app.
如上所述,您可以注册一个新的脚本块,也可以注册一个新的外部脚本文件。您还可以在插件中注册资产,这些资产将获取整个 JS 文件夹并将它们组合到您的应用程序中。
So there are many ways to use JQuery in Yii.
所以在 Yii 中有很多使用 JQuery 的方法。
How to use my jquery in yii
如何在 yii 中使用我的 jquery
JQuery comes pre-bundled and activated with JQuery and the JQuery library itself is considered a core script so there is no real need to put your own JQuery in.
JQuery 与 JQuery 预先捆绑并激活,并且 JQuery 库本身被认为是一个核心脚本,因此实际上没有必要放入您自己的 JQuery。
You can overwrite the built in JQuery with your own by putting:
您可以通过以下方式使用自己的 JQuery 覆盖内置的 JQuery:
$cs = Yii::app()->clientScript;
$cs->scriptMap = array(
'jquery.js' => Yii::app()->request->baseUrl.'/js/jquery.js',
'jquery.yii.js' => Yii::app()->request->baseUrl.'/js/jquery.min.js',
);
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('jquery.ui');
Something like that somewhere within either your layout, action or view. I prefer to put this within the layout and manage my own JQuery version personally.
在您的布局、操作或视图中的某个地方。我更喜欢把它放在布局中并亲自管理我自己的 JQuery 版本。
Why is this any different from using jQuery in any other way?
为什么这与以任何其他方式使用 jQuery 有什么不同?
Mainly due to standardisation and interoperability of the framework and its components. Coming pre-bundled with built in hooks for JQuery UI widgets and JQuery elements in general makes the framework much easier to build reusable and clean JS objects for use in your app.
主要是由于框架及其组件的标准化和互操作性。预捆绑了 JQuery UI 小部件和 JQuery 元素的内置钩子,使框架更容易构建可重用和干净的 JS 对象以在您的应用程序中使用。
Also I like JQuery so it makes sense for it to come pre-bundled in my opinion. However this is not a call to arms for JQuery being the only JS library/framework out there and you should really consider which is best for you before you choose.
我也喜欢 JQuery,所以在我看来预先捆绑它是有意义的。然而,这并不是对 JQuery 是唯一的 JS 库/框架的呼吁,在选择之前,您应该真正考虑哪个最适合您。
回答by Marian Zburlea
This is a sample script that I just used in my app development:
这是我刚刚在应用程序开发中使用的示例脚本:
<?php
$js = "
$('body')
.on('click', '#cancelCreateInductionTemplate, #closeDialogBox', function (e) {
// Close #openDialog
e.preventDefault();
$('#openDialog').dialog('close');
});
";
Yii::app()->clientScript->registerScript('cancelCreateInductionTemplate', $js, CClientScript::POS_READY);
?>