CodeIgniter 和 Javascript/Jquery 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5051059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:25:42  来源:igfitidea点击:

CodeIgniter and Javascript/Jquery Library

phpjavascriptjquerycodeigniterlibraries

提问by Luciano

As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.

正如标题所说,我试图弄清楚如何在 CI 上使用 javascript 和 jquery 库。

Following instruction in the docs, I load the library in my controller:

按照文档中的说明,我将库加载到我的控制器中:

$this->load->library('javascript');

Then, I define the location of the jQuery file (jquery.min.js) in the config.php:

然后,我在 config.php 中定义 jQuery 文件(jquery.min.js)的位置:

$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');

After that, I open the view file and put in these two lines:

之后,我打开视图文件并输入以下两行:

<?php echo $library_src;?>
<?php echo $script_head;?> 

First error comes up here: Undefined variable $library_src and $script_head(don't understand where I have to set them)

第一个错误出现在这里:未定义变量 $library_src 和 $script_head(不明白我必须在哪里设置它们)

Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:

无论如何,我已经评论了这些行并继续使用 jquery lib,通过在我的控制器中加载它:

$this->load->library('jquery');

Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)

下一个错误:无法加载请求的类:jquery。(似乎找不到lib,我搞砸了什么?)

Checking on system folder it looks all files are in place:

检查系统文件夹,它看起来所有文件都已就位:

system/libraries/Javascript.php
system/libraries/javascript/Jquery.php

Thanks in advance for your help!

在此先感谢您的帮助!

采纳答案by jondavidjohn

It is important to note that this Driveris marked as experimentalso I wouldn't rely on it.

需要注意的是,这个驱动程序被标记为实验性的,所以我不会依赖它。

Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.

此外,我个人认为尝试将应用程序的服务器端部分与客户端部分直接混合会引起混淆和头痛。

To use javascript in your views, I would just start out by loading them like this...

要在您的视图中使用 javascript,我只需像这样加载它们...

<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>

回答by zecohsox

Put the code in the config.php like this:

将代码放在 config.php 中,如下所示:

$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';

In your controller file (e.g. controllers/sample.php) type this codes:

在您的控制器文件(例如,controllers/sample.php)中输入以下代码:

 function __construct()
   {
        parent::__construct();
            $this->load->library('javascript');                    
   }

function index()
{

    $data['library_src'] = $this->jquery->script();
    $data['script_head'] = $this->jquery->_compile();

    $this->load->view('sampleview', $data);

}

In your view file (e.g. views/sampleview.php) type this codes:

在您的视图文件(例如 views/sampleview.php)中输入以下代码:

<?php echo $library_src;?>
<?php echo $script_head;?>

This works for me. I hope it works for you too. XD

这对我有用。我希望它也适用于你。XD

回答by R Down

Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.

因为这个驱动程序是实验性的,所以文档还不是很完整。但我能够找到解决方案。

First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not$script_headbut in fact$script_foot.

首先,文档中有错误。除非你改变了核心JavaScript库(不推荐)的参考变量是不是$script_head其实$script_foot

Second, once you've finished making your calls, it seems you need to run

其次,一旦你完成你的呼叫,你似乎需要运行

$this->javascript->external();

and

$this->javascript->compile();

These functions set the $library_srcand $script_footvariables.

这些函数设置$library_src$script_foot变量。

To put it all together, in your controller you would have:

总而言之,在您的控制器中,您将拥有:

class Some_Controller extends CI_Controller {
   public function index()
   {
       $this->javascript->click('#button', "alert('Hello!');");
       $this->javascript->external();
       $this->javascript->compile();
       $this->load->view('index');
   }
}

In your view you would have

在你看来,你会有

<html>
  <head>
     <?php echo $library_src; ?>
     <?php echo $script_foot; ?>

回答by Sarfraz

Although CI first looks for systemfolder, you can also try putting your libs in the these folders:

虽然 CI 首先查找system文件夹,但您也可以尝试将库放在这些文件夹中:

application/libraries/Javascript.php
application/libraries/javascript/Jquery.php

回答by Dev1410

As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .

正如我们在用户指南中所知道的,首先,这是一个实验性的但有效的。第一步是打开 application/config/config.php 下的配置文件。

Put the following line :

放置以下行:

// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'

$config['javascript_location'] = 'js/yourfile.js';

Second step is open your controller, within constructor method, put the following code :

第二步是打开您的控制器,在构造函数方法中,输入以下代码:

// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery

Then, still in controller file within index method :

然后,仍然在 index 方法中的控制器文件中:

$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().

Then open your view file and put the following code within tag head :

然后打开您的视图文件并将以下代码放入标签 head 中:

<?php echo $library_src; ?>

That way is working for me, let's try it.

这种方式对我有用,让我们试试看。

回答by rishabh shah

Use:

用:

$this->load->library('javascript/jquery');

instead of:

代替:

$this->load->library('jquery');

This will load your jQuery library.

这将加载您的 jQuery 库。

回答by wdphd

Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.

不知道为什么你必须通过控制器加载你的 js 文件,然后在你的视图中回显它。您可以直接在您的视图中使用 HTML 标签加载您的 js 文件。当变量的值是动态的/需要从数据库等加载时,主要使用从控制器传递数据并将其回显到视图中。

回答by Nils BAY

I had the same problem, and found :

我遇到了同样的问题,发现:

<?php $this->load->library('javascript'); ?>
<?php $this->load->library('javascript/jquery'); ?>

on https://ellislab.com/forums/viewthread/181742/#860506

https://ellislab.com/forums/viewthread/181742/#860506

Because jquery is in javascript folder.

因为 jquery 在 javascript 文件夹中。

Or

或者

$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');

In the autoload.php file.

在 autoload.php 文件中。

That solved the problem of loading the librairy.

这解决了加载库的问题。

回答by empugandring

You can try this, it's work to me.
in config.php

你可以试试这个,它对我有用。
在 config.php

$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';

in Controller

在控制器中

 public function __construct() {
    parent::__construct();
    $this->load->library('javascript');
    $this->load->library('javascript/jquery');
}

 public function index(){
    $d['library_src'] = $this->jquery->script();
    $d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
    $this->load->view('main',$d);
 }

in view (head)

在视图中(头)

<?php echo $library_src; ?>

in view content (body)

在视图内容(正文)

<div class="alert alert-dismissable alert-info" id="logalert">                          
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    </div>

in javascript view

在 JavaScript 视图中

<script type="javascript">
$(document).ready(function(){ 
   <?php echo $logalert; ?>
};
</script>

happy coding :)

快乐编码:)