CodeIgniter 和 Javascript/Jquery 库

发布于 2024-10-18 04:47:50 字数 921 浏览 0 评论 0原文

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

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

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

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

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

之后,我打开视图文件并放入这两行:

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

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

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

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

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

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

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

提前感谢您的帮助!

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

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['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)

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

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

今天小雨转甜 2024-10-25 04:47:50

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

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

在您的控制器文件(例如,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);

}

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

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

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

Put the code in the config.php like this:

$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:

 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:

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

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

把回忆走一遍 2024-10-25 04:47:50

请务必注意,此驱动程序被标记为实验,因此我不会依赖它。

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

要在你的视图中使用 javascript,我首先要像这样加载它们......

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

It is important to note that this Driver is marked as experimental so 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...

<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>
睫毛溺水了 2024-10-25 04:47:50

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

首先,文档中有错误。除非您更改核心 Javascript 库(不建议),否则引用变量不是$script_head,而是$script_foot

其次,一旦完成调用,似乎您需要运行

$this->javascript->external();

这些

$this->javascript->compile();

函数设置 $library_src$script_foot 变量。

要将所有这些放在一起,在您的控制器中您将拥有:

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');
   }
}

在您看来,您将拥有

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

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_head but in fact$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_src and $script_foot variables.

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; ?>
救赎№ 2024-10-25 04:47:50

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

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

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

application/libraries/Javascript.php
application/libraries/javascript/Jquery.php
你丑哭了我 2024-10-25 04:47:50

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

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.

悸初 2024-10-25 04:47:50

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

放置以下行:

// 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';

第二步是打开控制器,在构造函数方法中,放置以下代码:

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

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

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

然后打开视图文件并将以下代码放置在标记头中:

<?php echo $library_src; ?>

这种方式适用于我,我们试试吧。

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 .

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 :

$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 :

<?php echo $library_src; ?>

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

佞臣 2024-10-25 04:47:50

使用:

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

而不是:

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

这将加载您的 jQuery 库。

Use:

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

instead of:

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

This will load your jQuery library.

终陌 2024-10-25 04:47:50

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

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

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

因为 jquery 位于 javascript 文件夹中。

或者

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

在 autoload.php 文件中。

这样就解决了加载库的问题。

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

Because jquery is in javascript folder.

Or

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

In the autoload.php file.

That solved the problem of loading the librairy.

被翻牌 2024-10-25 04:47:50

你可以试试这个,这对我来说很有用。
在config.php中

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

在控制器中

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

在视图(头)

<?php echo $library_src; ?>

中在视图内容(正文)

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

中在javascript中查看

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

快乐编码:)

You can try this, it's work to me.
in 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">×</button>
    </div>

in javascript view

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

happy coding :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文