简体   繁体   English

如何在Yii框架中使用jQuery。

[英]How to use jQuery with the Yii framework.?

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有什么不同?

using Yii's registerScript to load our jQuery, which if used normally looks like this: 使用Yii的registerScript加载我们的jQuery,如果通常使用的话如下所示:

$(document).ready(function(){
    alert('hello');
    // Enter code here
});

Now if we use Yii's Script like 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; 您将看到需要将3个内容传递给Yii :: app() - > clientScript-> registerScript函数,首先是脚本名称,这并不重要; it must just bee unique from any other script name on the same page. 它必须与同一页面上的任何其他脚本名称唯一。 LINK 链接

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:

$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. 我也喜欢JQuery,所以在我看来它预先捆绑是有道理的。 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作为唯一的JS库/框架的呼吁,你应该在选择之前考虑哪个最适合你。

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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM